This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: Problem with comparisons



Tool/software: TI C/C++ Compiler

I'm wanted to check if a given value is in a certain interval. Some thing like :

if (weight > target - delta && weight < target + delta)

Worked fine except when my target is 0; the second test fails although the values are correct. The real code is using more intricate functions so I tried to isolate the issue:

        uint32_t weight = 4;
        int16_t target = 0;
        uint8_t delta = 8;

        if (weight > target - delta)
           terminal << "OK\n";
       else
            terminal << "WRONG !!! \n";

I believe 4 is bigger than 0-8, but the code does not agree, it will go into the else branch and write WRONG !!!.

If I simplify even further:

      if (weight > -8)
           terminal << "OK\n";
       else
            terminal << "WRONG !!! \n";

The compiler even removes the test, assuming it's wrong (see picture below).

I assume there's a type mismatch issue here, but I have not been able to obtain a correct result.