Thread: Finding a reference error by mathematical conditioning

  1. #1
    Registered User
    Join Date
    Aug 2021
    Posts
    12

    Angry Finding a reference error by mathematical conditioning

    Hi There,


    I am a new comer here. I just want to know few things those are really hard to track down.I am trying to debug a existing F/W code mostly written in c or c++. This IDE and compiler is from a particular DSP/MCU company. This code is a complex one and F/W and S/W mixed together. Some codes are not even understandable because I have very limited knowledge in syntax, pointer datatype and flags etc.Manuals and examples can give some ideas.

    Now lets talk a specific problem, .c file includes some dependencies like those are customized,made for particular reason

    Code:
    #include "DSP28x_Project.h"
    #include "Register.h"
    #include "CLA.h"
    #include "LED.h"
    From CLA (Control Law Accelerator? library of TI external VOID function with macro has called to compare some reference value of particular parameter. here we go,

    Code:
    __interrupt void Cla1Task2 ( void )
    {
        float Err;
    
        Register[ADC_7] = AdcResult.ADCRESULT7;// this is from instructional manual that specified the ADC result location
    
        Bf7();// this is a function works for some float variables 
    
        Current_T_ch2     = ADC_VAL(Register[ADC_FILT_7], Register[ADC_ZERO_7], Register[ADC_SCALE_7]);// ADC_VAL is defined somewhere
        Current_T_ch2 = Current_T_ch2 < 0 ? 0 : Current_T_ch2;
    
        if(Tartget_B2 > 0)
        {
            Err = Tartget_B2 - Current_T_ch2;// target B2 is also a register.
            B_Ref2 += PKP2 * (Err - Err_B_P2) + PKI2 * Err;// PKI2 is defined in a register file 
            Err_B_P2 = Err;
    
            B_Ref2 = B_Ref2 > 0.35 ? 0.35 : (B_Ref2 < 0 ? 0 : B_Ref2);// this one is not understandable 
        }
    }
    

    Let me know the mathematical reasoning like, x= x>0.35 ? 0.35 : ( x<0 ? 0 : x);
    How ?, : is meaning full? How does compiler read and think? if x> 0.35 then how the condition applies for x<0
    Last edited by Hasan2021; 08-18-2021 at 07:29 PM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    '?' is the ternary operator.

    It is clamping x to between 0.0 an 0.35

    Translated:

    If x > 0.35 then
    Use 0.35

    Else If x < 0.0 then
    Use 0.0

    Else
    Use x.

  3. #3
    Registered User
    Join Date
    Aug 2021
    Posts
    12
    Quote Originally Posted by hamster_nz View Post
    '?' is the ternary operator.

    It is clamping x to between 0.0 an 0.35

    Translated:

    If x > 0.35 then
    Use 0.35

    Else If x < 0.0 then
    Use 0.0

    Else
    Use x.

    Nice feedback indeed.
    Does : means that we can place this 2 condition at a time?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    It is a pretty common pattern that people use. Not because it is particularly nice, but just because it fits on one line.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2021
    Posts
    12
    Some time people overlook silly question.
    Does it offence to somewhere else?
    If so pardon me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the error
    By DianaOli in forum C Programming
    Replies: 6
    Last Post: 01-16-2017, 01:30 PM
  2. Replies: 10
    Last Post: 05-15-2011, 06:52 PM
  3. Replies: 5
    Last Post: 04-07-2011, 10:40 PM
  4. Mathematical Error
    By toonlover in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2006, 10:21 PM

Tags for this Thread