Logic Behind the Convention of the Result of Modulo Operations

20 Views Asked by At

Consider the following 4 results

 7 %  3 =  1
-7 %  3 =  2
 7 % -3 = -2 
-7 % -3 = -1

I'm struggling to see why these outputs have been selected as such

7 % 3 is clear. Imagining | as 0, I can chunk up 7 into 3 groups of 3, and I have 1 left over going away from |

|*** *** *** *

Of course, we could say this is -1 as in "take away 1 from your numerator to make it divisible" or we could say it's -2 or 2, they all represent in some way how far off we are from having a perfect divisor.

The convention for -7 % 3 is odd to me, but I can reason about it. I would've thought it was -1 (3 groups of 3 and a remaining -1), but it's 2. I guess that's just convention.

* *** *** ***|

I'm really struggling reasoning about 7 % -3 and -7 % -3, though. Can someone explain this to me so I can intuitively understand this operation more quickly?

Side note, if I were creating a remainder operator, it would have these results

 7 %  3 =  1 # remaining 1
-7 %  3 = -1 # remaining -1
 7 % -3 = -1 # -ve version of 7 % 3
-7 % -3 =  1 # same as 7 % 3