So the magic formula seems right.
If you define a // b as floor(a / b) (which it is), a % b should be a - floor(a / b) * b . Let's get a look:
a: -10b: 3a % b = a - floor(a / b) * b = -10 - floor(-3.33) * 3 = -10 + 4 * 3 = 2
The fact that a // b always overlapping is pretty easy to remember (please read the first link of Cthulhu, this is an explanation of the creator of Python). For negative a in a % b .. try to imagine a table of numbers that starts at 0 and has columns b :
b = 3: 0 1 2 3 4 5 6 7 8 9 10 11 ...
If a is the number in the cell, a % b will be the column number:
aa % b _______________ 0 1 2 0 1 2 3 4 5 0 1 2 6 7 8 0 1 2 9 10 11 0 1 2
Now add the table back to the negatives:
aa % b __________________ -12 -11 -10 0 1 2 -9 -8 -7 0 1 2 -6 -5 -4 0 1 2 -3 -2 -1 0 1 2 0 1 2 0 1 2 3 4 5 0 1 2 6 7 8 0 1 2 9 10 11 0 1 2
-10 % 3 will be 2 . A negative a in a % b would appear under such conditions. a % b with negative b not very much.
source share