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\%n + 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.
- 282,220
- 65,209
-
I think Euclidean division is horrific.
7 div 5, 7 mod 5versus-7 div -5, -7 mod -5ends up with BOTH a different quotient as well as a different modulo. Ditto for-7 div 5, -7 mod 5versus7 div -5, 7 mod -5. Floored division is far more self consistent in that regard. Euclidean division is the only one that fails to even respect a simultaneous negation of dividend and divisor, which should result in the same quotient, since that's just rephrasing the multiplicative identity function1as(-1) / (-1). – RARE Kpop Manifesto Aug 21 '24 at 14:59 -
They can call it "Euclidean modulo operation" but shouldn't call it Euclidean "division", since the operation itself is highly self-inconsistent in order to achieve the arbitrary criteria of always non-negative modulo. I mean there's a good reason why even the dedicated math engine WolframAlpha doesn't use Euclidean division for modulo ops. – RARE Kpop Manifesto Aug 21 '24 at 15:05
-
A concrete example, rephrasing the one u used above, would be
if one is required to submit homework by end of this 7 hour window, and someone submitted it 2 hours prior to start of window, Euclidean division ends up sayingbeing super early and submitting 2 hours prior to start of window, is equivalent to being 5 hours on time??????. IfFAAused Euclidean division, then a flight arriving20 mins earlyis equivalent to a flight arriving40 mins late????? – RARE Kpop Manifesto Aug 21 '24 at 15:12