This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[C51]Bug report: signed char divided by power of 2

Bug fact: Following code cannot get right results.

#include "SST89x5xxRD2.H"

signed char x,y,z;

void main()
{
        x = -15;
        y = x / 4;
        z = x % 4;

        while(1);
}

y = -4 and z = -3, while y should be -3...

Reason: Compiler automatically generates RRC for division by power of 2. For a SIGNED CHAR, this could produce unexpected rounding results.

Solution: If a signed char is divided by power of 2, DO NOT use RRC in this case.

Parents
  • "as KeilC51 library functions choose truncating TOWARD ZERO, isn't it a bug as the compiler truncates AWAY FROM ZERO in this rare situation?"

    No.

    As already noted, the compiler can do whatever it likes for the division. The library function is well-defined - but there is no requirement for it to be the same!

Reply
  • "as KeilC51 library functions choose truncating TOWARD ZERO, isn't it a bug as the compiler truncates AWAY FROM ZERO in this rare situation?"

    No.

    As already noted, the compiler can do whatever it likes for the division. The library function is well-defined - but there is no requirement for it to be the same!

Children