should negative 0 be an issue in this situation?

-(b % c) cannot generate a negative 0 because C 2018 6.2.6.2 3 says:

If the implementation supports negative zeros, they shall be generated only by:

— the &, |, ^, ~ , << , and >> operators with operands that produce such a value;

— the +, - , *, /, and % operators where one operand is a negative zero and the result is zero;

— compound assignment operators based on the above cases.

Even if we interpret the above to include - as a unary operator, we have been given that b and c are positive integers, so b % c cannot produce a negative zero, so the operand to - in -(b % c) is not a negative zero.

If == is used to compare a negative zero and a non-negative zero, it produces true, as == operates based on the values of the operands, not their representations.

Negative zeros are extremely rare for integers in modern C implementations. Modern implementations overwhelmingly use two’s complement, which does not have a negative zero. Only specialized/historic/archaic implementations might use the alternative integer representations that have negative zeros (one’s complement and sign-and-magnitude). Floating-point representations commonly have negative zeros, but they are equal to positive zeros in comparisons for equality by value (the ==, !=, <, >, <=, and >= operators).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top