There are three definition of modulo operations: Truncated, Floored, and Euclidean. Their difference is how to handle negative numbers.

Example

TruncatedFlooredEuclidean
-4 % 4000
-3 % 4-311
-2 % 4-222
-1 % 4-133
0 % 4000
1 % 4111
2 % 4222
3 % 4333
4 % 4000
TruncatedFlooredEuclidean
-4 % -4000
-3 % -4-3-31
-2 % -4-2-22
-1 % -4-1-13
0 % -4000
1 % -41-31
2 % -42-22
3 % -43-13
4 % -4000

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).

See also

References

Footnotes

Footnotes

  1. Modulo - Wikipedia