As a general rule, the modulo and division should satisfy the equation
b * (a/b) + a%b == a
For positive numbers, it is obvious that this means that a%b
must be a positive number. But if a/b
is negative, then the result is rounded towards zero.
So take for instance a = -4, b = 3. We know that a/b = -1.3333, which rounded towards zero becomes a/b == -1
. From the equation above, we have that b * (-1) + a%b == a
. If we insert a
and b
, we get -3 + a%b == -4
, and we see that a%b
must be -1.