Math vs Programming Modulo: The Negative Number Problem
The divergence between math and programming modulo conventions stems from how they handle integer division. Mathematical modulo uses floor division (rounding toward negative infinity), so the quotient of negative 7 divided by 3 floors to negative 3, giving remainder negative 7 minus 3 times negative 3 equals 2. Programming modulo uses truncated division (rounding toward zero), so the quotient truncates to negative 2, giving remainder negative 7 minus 3 times negative 2 equals negative 1.
Python uniquely uses mathematical (floored) modulo, matching the convention expected in abstract algebra and number theory. C, C++, Java, JavaScript, Go, and Rust use truncated modulo. Ruby and Perl match Python's behavior.
This distinction matters in real code: converting a negative angle to the range 0-359 requires math modulo (negative 90 mod 360 equals 270), but C's percent operator gives negative 90, requiring manual adjustment.