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
  • According to my book (Harbison and Steele): "Prior to C99, C implementations could choose to truncate toward or away from zero if either of the operands were negative. The div and ldiv library functions were always well defined for negative operands."

    Since C51 is not (fully?) C99 compliant, it looks like this is not a bug at all.

Reply
  • According to my book (Harbison and Steele): "Prior to C99, C implementations could choose to truncate toward or away from zero if either of the operands were negative. The div and ldiv library functions were always well defined for negative operands."

    Since C51 is not (fully?) C99 compliant, it looks like this is not a bug at all.

Children