[Math] Modulus Operation with Negative Numbers

abstract-algebramodular arithmetic

This topic has been asked many times and I have seen all the answers but none of them was helpful in clearing my doubt which is why this question is here. First of all, let's get the definitions out of the way.

Division Algorithm:

Let $a$ and $b\ (\neq0)$ be any integers. Then there exist unique integers $q$ and $r$ with the property that $a=bq+r$, where $0 \leq r \lt |b|$

$\bmod{}$ Operator:

For the same $a,b,q$ and $r$ as stated in the definition above, we say
$$a \bmod b=r$$

Note that $a,b$ and $q$ can either be positive, negative or even $0$ (if we exclude $b$) whereas $r$ can only be non-negative.

Case $1$: Positive Divisor and Positive Dividend:
$$68 \bmod 12=8$$
Reasoning: $68=12(5)+8$

Case $2$: Positive Divisor and Negative Dividend:
$$-68 \bmod 12=4$$
Reasoning: $-68=12(-6)+4$

Case $3$: Negative Divisor and Positive Dividend:
$$68 \bmod -12=8$$
Reasoning: $68=-12(-5)+8$

Case $4$: Negative Divisor and Negative Dividend:
$$-68 \bmod -12=4$$
Reasoning: $-68=-12(6)+4$

Doubt: Are all the cases that I listed sound along with their reasoning? If not, please show me how to evaluate$\bmod{}$correctly. Also, would you like to add something which would enhance my knowledge of modulo arithmetic or anything in general?

Best Answer

Your answers are correct.

There's not much to say about what you've written. One quibble: Below your definition you mention that $r$ must be positive but that's not correct as $r$ is allowed to be $0$. Of course, in the definition you correctly state the bounds of $r$ as $0 \leq r < |b|$.

One other thing to note is that the sign of $b$ is irrelevant because if $a = bq + r$ then also $a = (-b)(-q) + r$. Hence using the definition stated it's always the case that $a \bmod b = a \bmod (-b)$.

Defining mod as an operator as you have is perfectly fine but you should be aware that it is often used as a modifier for the entire equation and not strictly as an operator. So, for example, mathematicians would generally write $9 = 15 \bmod 6$ instead of $9 \bmod 6 = 15 \bmod 6$. You may also see $\equiv$ used instead of $=$. You can be technical about what $\equiv$ means in this context, but for your purposes I wouldn't worry too much about those technicalities yet, just know that the intention is often that you apply your mod operator to both sides of the equation.

Finally, note that mod as an operator exists in many computer programming languages but in those languages it does not always follow your definition exactly. Some languages will return negative $r$ when $a$ is negative. This is an implementation choice that's made differently for each language; there's no deep mathematical significance to this decision.