Division and Modulus

To ensure that the results of the "/" and "%" operators are defined correctly for signed arithmetic operations, both operands must be defined positive. (for more information, refer to the backend chapter.) It is implementation-defined if the result is negative or positive when one of the operands is defined negative. This is illustrated in the following listing.

Listing: Effect of polarity upon division and modulus arithmetic.
#ifdef __MODULO_IS_POSITIV__
 22 / 7 ==  3;   22 %  7 == 1

 22 /-7 == -3;   22 % -7 == 1

-22 / 7 == -4;  -22 %  7 == 6

-22 /-7 ==  4;  -22 % -7 == 6

#else

 22 / 7 ==  3;   22 %  7 == +1

 22 /-7 == -3;   22 % -7 == +1

-22 / 7 == -3;  -22 %  7 == -1

-22 /-7 ==  3;  -22 % -7 == -1

#endif

The following sections show how it is implemented in a backend.