Please help me understand why the modulus works this way.

86 Views Asked by At

I am very confused by the following equation:

$$ \mathbb {-1\%4=3}$$

I understand that the modulo or modulus operator gives the remainder of a division. This is not new or a mystery to me. What I am confused about is how the remainder is the integer 3. This does not make logical sense to me.

For instance, if I do -1 / 4 I get -0.25 shown in the following equation: $\mathbb {-1 / 4 = -0.25} $ Where is the 3 in the remainder?

Or check this out, if I calculate the modulus of another couple of numbers the answer is much simpler for me to understand. Check this out: $$\mathbb {5 \% 3 = 2} $$

This is obvious that the remainder is 2 because $\mathbb {5 / 3 = 1 R2}$ The R2 means remainder of 2. So, in other words 5 / 3 = 1 with a remainder of 2. This makes sense why the modulo is of 5 % 3 is 2 based on the result of the division.

I am confused though why $\mathbb {-1 / 4 = -0.25}$ but $ \mathbb{-1 \% 4 = 3}$. Can you help me understand this?

Thank-you for reading this.

3

There are 3 best solutions below

4
On BEST ANSWER

There is a subtle difference between the usage of $=$ (equality) and $\equiv$ (equivalence).

We say $$a\equiv b\pmod c\implies a=b+cx, x\in\Bbb Z.$$

All integers will fall into one of four categories: $0, 1, 2$, or $3\pmod 4$, depending on which one of these numbers you have to add to some multiple of $4$ you have to get your integer. In your case, in order to get $-1$, you have to add $3$ to $-4$, and therefore, we have $-1\equiv 3\pmod 4$.

1
On

The modulus operator $a\%b$ in your computer language will always produce a number between $0$ and $b-1$ (assuming $b > 0$). It looks at all the numbers $$ \ldots, a-2b, a-b, a, a+b, a+2b, \ldots $$ and returns the one between $0$ and $b-1$.

If $a > 0$ you can find that value with ordinary division with remainder. You should not be thinking about decimal fractions for $-1\%4$.

2
On

If $y = a x + b$ where $y, x, a, b$ are integers (with $x > 0$) and $0 \le b < x$, then we say $b$ is the remainder on dividing $y$ by $x$, denoted $y \% x$.

In this case you write $-1/4 = -.25$, but in this case it would be better to write it as $-1 + 3/4$.