I have two questions about using modulation in equations. My first question is what notation is the right to use (i.e. x%y or mod(x, y))? The second is what are its properties for adding, multiplying, etc? Any response would be great. Thanks.
-
1The programming $x%y$ corresponds to the more standard mathematical notation $x\bmod y$. – Brian M. Scott Oct 28 '14 at 20:45
-
See here and here for discussion of mod as a binary operator vs. equivalence relation. – Bill Dubuque Feb 19 '20 at 16:17
2 Answers
Notation: $b=a\bmod m$, and $b\equiv a\mod m.$
Addition: $$\color{red}{(a\pm b)\bmod m\ne a\bmod m\pm b \bmod m}.$$ $$(a\pm b)\bmod m=(a\bmod m\pm b \bmod m)\bmod m.$$
Multiplication: $$\color{red}{(a.b)\bmod m\ne(a\bmod m).(b\bmod m)}.$$ $$(a.b)\bmod m=(a\bmod m).(b\bmod m)\bmod m.$$
Integer division: $$\color{red}{(a\div b)\bmod m\ne(a\bmod m)\div (b \bmod m)}.$$ $$\color{red}{(a\div b)\bmod m\ne(a\bmod m)\div (b \bmod m)\bmod m}.$$
Modulo: $$\color{red}{(a\bmod m)\bmod n\ne a\bmod(m \bmod n)}.$$ $$\color{red}{(a\bmod m)\bmod n\ne a\bmod(m.n)}.$$ $$(a\bmod m)\bmod n=(a\bmod n)\bmod m.$$
Quotient/remainder decomposition: $$a=(a\div m).m+a\bmod m.$$
Comparison: $$\color{red}{a\bmod m=b\bmod m\nRightarrow a=b}.$$ $$a=b\Rightarrow a\bmod m=b\bmod m.$$ $$\color{red}{a\bmod m>b\bmod m\nRightarrow a>b}.$$ $$\color{red}{a>b\nRightarrow a\bmod m>b\bmod m}.$$
Miscellaneous: $$0\le a\bmod m<m.$$ $$gcd(a,b)=gcd(b,a\bmod b).$$ $$(a.m+b)\bmod m=b\bmod m.$$ $$(a\bmod m)\bmod m=a\bmod m.$$
A very important result is the Chinese Remainder Theorem.
Let me give a stuffy mathematician’s answer.
The notations $x\%y$ and $\text{mod}(x,y)$ are programmers’ or computer scientists’ notations, not mathematicians’. For me, they are equally good or bad, and it’s a matter of choice which to prefer. If you work from different sources or texts, you should be prepared to see either at any time.
The way $m\%n$ is defined (for an integer $m$ and a positive integer $n$) is that the result is an integer in the range $\{0\dots,n-1\}$, equal to the remainder of $m$ upon Euclidean division by $n$. That is, there are unique quotient $q$ and remainder $r$ with $m=nq+r$, and $0\le r<n$. The $r$ here is your $m\%n$. Thus, $(-2)\%7=5$.
The behavior of “$\%$”, not exactly amounting to rules, is as follows: $(a+b)\%n=[a\%b+b\%n]\%n$, and exactly the parallel thing for subtraction and multiplication. The operation does not usually behave well with respect to any kind of division. Notice that these are very ugly rules, and perhaps this is why “$\%$” is not used by mathematicians.
Rather, mathematicians use the equivalence relation $\text{(one integer)}\equiv\text{(another integer)}\pmod n$, for example $-2\equiv5\pmod7$. And the definition is that $a\equiv b\pmod n$ if $b-a$ is an integral multiple of $n$. Now the phenomena have a very neat description: if $a\equiv a'\pmod n$ and $b\equiv b'\pmod n$, then $a+b\equiv a'+b'\pmod n$ and $ab\equiv a'b'\pmod n$.
Finally, the connection between the two approaches is that if $m$ and $n>0$ are given, then there is a unique integer $r$ in the range $0\le r<n$ such that $r\equiv m\pmod n$, and this $r$ is your programmer’s $m\%n$.
Maybe I should also say that there are extensions of the notation and the concept to nonintegers, but the description above is the original and fundamental one.

- 62,818