Three conventions, one definition
Every "mod" operation must satisfy a = q·b + r. The three conventions differ only in how they round q:
- Truncated — round q toward zero. The remainder takes the sign of a. This is what C, Java and JavaScript's
%operator returns. - Floored — round q toward −∞. The remainder takes the sign of b. Python's
%uses this; it's the "mathematical" convention on the integers. - Euclidean — choose q so that the remainder is always in [0, |b|). Used in number theory; ensures r ≥ 0 regardless of signs.
For positive a and b all three give the same answer. They diverge only when at least one input is negative.
FAQ
Why do different languages give different signs?
Most C-family languages (C, C++, Java, JavaScript) chose truncated division for performance; Python and Ruby chose floored division for nicer mathematical properties. The calculator shows all three so you can match the language you're working in.
Does modulo work for decimals?
Yes. 5.7 mod 0.4 = 0.1 in all three conventions. The same definition applies but q is rounded as before.
What is "modular arithmetic" used for?
Cryptography (RSA, hashing), cyclic patterns (clock arithmetic, day-of-week math), and detecting divisibility (n is even ⇔ n mod 2 = 0).