SEARCH HOME
 Math Central Quandaries & Queries
 I am a teacher teaching computer science using Turing. I am having difficulty understanding why one would use the mod operator versus the rem remainder operator. Mod seems to make the resulting sign depend on the sign of the divisor, whereas rem makes the resulting sign depend on the dividend. Examples: 11 mod 5 = 1 and 11 rem 5 =1 -11 mod 5 = 4 and -11 rem 5 = -1 11 mod -5 = -4 and 11 rem -5 =1 -11 mod -5 = -1 and -11 rem -5 = -1 What I can't understand is why this would matter. For example, -11 / 5 = -2.2 and 11 / -5 = -2.2 get the same result. So how is a remainder dependent on the sign of one of the parts? What benefit would using one over the other have? Any insight would be most helpful! Eric

Hi Eric,

The operator rem $a$ is returning the remainder on division by $a.$ In your example

\begin{eqnarray}
11 & = & \; \; 2 & \times & \; \; 5 & + 1 & \\
-11 & = & -2 & \times & \; \; 5 & -1 &\\
11 & = & -2 & \times & -5 & +1 &\\
-11 & = & \; \; 2 & \times & -5 & -1 &.
\end{eqnarray}

The operator mod $a$ is one that is used a great deal in number theory, but almost exclusively with $m$ a positive integer. We say that a is congruent to b modulo m if b - a is divisible by m, and denote the relationship by

$a \equiv b \pmod{m}$

Thus

$11 \equiv 1 \pmod{5}$

but so is

$11 \equiv 6 \pmod{5}$

and

$11 \equiv -4 \pmod{5}$

If you are asked to solve $11 \equiv x \pmod{5}$ for $x,$ which one should you return? The convention is to return the value of $x$ that satisfies the congruence and lies between $0$ and $m.$ More explicitly, when $m > 0, x$ satisfies $0 \leq x < m$ and when $m < 0, x$ satisfies $m < x \leq 0.$

I hope this helps,
Harley

Math Central is supported by the University of Regina and The Pacific Institute for the Mathematical Sciences.