There are three definition of modulo operations: Truncated, Floored, and Euclidean. Their difference is how to handle negative numbers.
Example
Truncated | Floored | Euclidean | |
---|---|---|---|
-4 % 4 | 0 | 0 | 0 |
-3 % 4 | -3 | 1 | 1 |
-2 % 4 | -2 | 2 | 2 |
-1 % 4 | -1 | 3 | 3 |
0 % 4 | 0 | 0 | 0 |
1 % 4 | 1 | 1 | 1 |
2 % 4 | 2 | 2 | 2 |
3 % 4 | 3 | 3 | 3 |
4 % 4 | 0 | 0 | 0 |
Truncated | Floored | Euclidean | |
---|---|---|---|
-4 % -4 | 0 | 0 | 0 |
-3 % -4 | -3 | -3 | 1 |
-2 % -4 | -2 | -2 | 2 |
-1 % -4 | -1 | -1 | 3 |
0 % -4 | 0 | 0 | 0 |
1 % -4 | 1 | -3 | 1 |
2 % -4 | 2 | -2 | 2 |
3 % -4 | 3 | -1 | 3 |
4 % -4 | 0 | 0 | 0 |
Different programming languages use different definition of modulo1. The advantage of Floored and Euclidean is that they can wrap around with negative numbers (think about negative array indices).