Understanding modular arithmetic
This short guide explains what the modulo calculator does, how to get the best result and when another tool is a better fit.
What modulo means
Modulo is the remainder after division. 17 divided by 5 is 3 with 2 left over, so 17 mod 5 = 2. It is often called clock arithmetic, because clocks wrap around: 10 hours after 17:00 is 27:00, and 27 mod 24 = 3, so it is 03:00. Calendars, angles and repeating patterns all use the same idea.
If a mod n = 0, then n divides a exactly.
Negative numbers
For negative numbers there are two common rules. The floored rule, used in maths and Python, always gives a result with the same sign as the divisor, so โ17 mod 5 = 3. The truncated rule, used by the % operator in JavaScript, C and Java, keeps the sign of the dividend, giving โ2. The calculator shows both so you can match the language youโre using.
When both answers agree, youโll see the same number twice.
Big numbers and checks
Whole numbers of any length are handled exactly using big integer maths, so 123456789012345678901234567890 mod 97 gives the precise answer, 52. The check line, n ร q + r = a, proves the result. Decimals use normal floating-point maths and are rounded to 12 significant digits to hide tiny rounding errors.
Spaces and commas in long numbers are ignored.
Practical uses
Programmers use modulo to test for even numbers (n mod 2), wrap list indexes and spread items across servers. Check digits on IBANs, ISBNs and many ID numbers are calculated with modulo, often mod 10, 11 or 97. Teachers use it to split pupils into groups and find who is left over.
Example: 25 pupils in groups of 7 gives 3 full groups and 25 mod 7 = 4 left over.
Related tools
For quotient and remainder together, try the Remainder Calculator. For lowest common multiples, use the LCM Calculator.
- a = n ร q + r
- Floored and truncated results differ for negatives
- Huge whole numbers are exact
Further reading: The modulo operation. It explains the format behind this modulo calculator in more detail.
