THE rem AND mod OPERATORS

Where as the rem operator produces the remainder after an integer division the modulus operator (mod) works slightly differently. Where both operands have the same sign, or where there is no remainder, there is no obvious difference:

Integer AInteger BA/BA rem BA mod B
105200
125222
145244
-10-5200
-12-52-2-2
-14-52-4-4
10-5-200
-105-200

However where only one of the operands is negative there operation is different:

Integer AInteger BA/BA rem BA mod B
12-5-22-3
14-5-24-1
-125-2-23
-145-2-41

A quick method of determining the modulous in this case is to add the remainder to the devisor (integer B):

-5  +   2  = -3
-5  +   4  = -1
 5  + (-2) =  3
 5  + (-4) =  1



Created and maintained by Frans Coenen. Last updated 11 October 1999