The modulo operation gives the remainder after division. 17 mod 5 is 2, because 5 goes into 17 three times with 2 left over. That much is simple, and it powers everyday calculations such as working out the day of the week, wrapping around a clock, or deciding whether a number is even. Then a negative number shows up, and suddenly Python says −17 mod 5 is 3 while JavaScript says −2. Both are "right", and understanding why saves hours of debugging.
This guide explains the definition behind modulo, the three conventions for negative numbers and which languages use each, clock arithmetic, and two advanced operations that appear in cryptography and competitive programming: modular exponentiation and the modular inverse.
The definition
For a number a and a divisor n, modulo finds a quotient q and remainder r such that:
a = n × q + r
For positive numbers there is only one sensible answer with 0 ≤ r < n. For 17 and 5: 17 = 5 × 3 + 2, so 17 mod 5 = 2.
The operation is written a mod n in mathematics and usually a % n in programming languages. Some languages use % as "remainder" and provide a separate function for "modulo", which is exactly where the confusion with negatives begins.
Negative numbers: three conventions
When a is negative, several (q, r) pairs satisfy a = n × q + r. The conventions differ in how they round the quotient a ÷ n.
| Convention | Quotient rounds | −17 mod 5 | 17 mod −5 | Used by |
|---|---|---|---|---|
| Truncated | towards zero | −2 | 2 | C, C++, Java, JavaScript %, Go, Rust |
| Floored | down (towards −∞) | 3 | −3 | Python %, Excel MOD, Ruby, R |
| Euclidean | so r is never negative | 3 | 2 | Number theory |
Work through −17 mod 5 by hand:
- −17 ÷ 5 = −3.4.
- Truncated: round towards zero to q = −3. Then r = −17 − 5 × (−3) = −2.
- Floored: round down to q = −4. Then r = −17 − 5 × (−4) = 3.
A handy summary: with truncation, the remainder takes the sign of the dividend; with flooring, it takes the sign of the divisor; with the Euclidean convention, it is always zero or positive.
Why it matters in code
A common bug is using i % n to wrap an array index. In JavaScript, (-1) % 5 is −1, which is not a valid index. The standard fix produces the floored or Euclidean result for a positive n:
((i % n) + n) % n
For i = −1 and n = 5 this gives 4, the last element, which is usually what you want.
Decimals and fractions
Modulo also works for non-integers. 5.5 mod 2 = 1.5, because 5.5 = 2 × 2 + 1.5. Some languages support this (Python, JavaScript), others do not for integer types (C's % requires integers; fmod handles floating point).
Clock and calendar arithmetic
Modulo is the mathematics of things that wrap around.
- Hours: 100 hours after 09:00 is (9 + 100) mod 24 = 109 mod 24 = 13, so 13:00, four days later.
- Days of the week: if today is Wednesday, 45 days from now is 45 mod 7 = 3 days further round the week: Saturday.
- Even and odd: n mod 2 is 0 for even numbers and 1 for odd numbers.
- Check digits: many ID numbers, including the ISBN-10 and IBAN systems, use a remainder to detect typing errors.
In mathematical notation, two numbers are congruent modulo n if they leave the same remainder: 17 ≡ 2 (mod 5).
Modular exponentiation
Cryptography and hashing often need values such as 7¹²⁸ mod 13. Calculating 7¹²⁸ first is hopeless (it has over 100 digits), and the numbers in real systems are far larger. The trick is that you can reduce after every multiplication:
(a × b) mod n = ((a mod n) × (b mod n)) mod n
Combine that with repeated squaring:
7² = 49 ≡ 10 (mod 13)
7⁴ ≡ 10² = 100 ≡ 9
7⁸ ≡ 9² = 81 ≡ 3
7¹⁶ ≡ 3² = 9
7³² ≡ 9² = 81 ≡ 3
7⁶⁴ ≡ 9
7¹²⁸ ≡ 3
Seven squarings replace 127 multiplications, and no number ever exceeds 13². The answer is 3. For exponents that are not powers of two, write the exponent in binary and multiply together the squares that correspond to its 1 bits.
Fermat's little theorem gives another shortcut when the modulus is prime: a^(p−1) ≡ 1 (mod p) if a is not a multiple of p. So 3²⁰⁰ mod 7 reduces to 3^(200 mod 6) = 3² = 9 ≡ 2.
The modular inverse
In ordinary arithmetic, the inverse of 3 is 1/3, because 3 × 1/3 = 1. In modular arithmetic, the inverse of a modulo m is a whole number x such that:
a × x ≡ 1 (mod m)
The inverse of 3 modulo 26 is 9, because 3 × 9 = 27 = 26 + 1. The inverse of 7 modulo 40 is 23, because 7 × 23 = 161 = 4 × 40 + 1.
An inverse exists only when a and m share no common factor (their greatest common divisor is 1). The number 4 has no inverse modulo 26, since both are even: 4x is always even, so it can never be one more than a multiple of 26.
For small numbers you can search by trial; for large ones the standard method is the extended Euclidean algorithm, which finds x as a by-product of computing the GCD. See The Euclidean Algorithm for how the GCD part works. Modular inverses are at the heart of RSA key generation and of simple ciphers such as the affine cipher, where decrypting requires the inverse of the multiplier modulo 26.
Using the modulo calculator
The Modulo Calculator has three tabs.
- a mod n: enter the Dividend (a) and the Divisor / modulus (n). Negatives, decimals and fractions are allowed. The result shows all three conventions side by side, labelled with the languages that use them (for example "Floored — Python %, Excel MOD, Ruby, R"), plus the floored quotient and the working for each.
- Power mod (aᵇ mod m): enter the Base (a), Exponent (b) and Modulus (m), with whole numbers of up to 400 digits. The result reports how many squarings and multiplications were needed compared with naive repeated multiplication.
- Modular inverse: enter the Number (a) and Modulus (m). You get the inverse with a check line (a × inverse ≡ 1), or an explanation when no inverse exists because the numbers share a factor.
Seeing the three conventions together is the fastest way to predict what a particular language will return before you write the code.
Other ways to calculate modulo
- Spreadsheets:
=MOD(-17, 5)returns 3 in Excel, LibreOffice and Google Sheets (floored). - Programming: Python's
pow(7, 128, 13)does fast modular exponentiation, and from Python 3.8pow(3, -1, 26)returns the modular inverse. In JavaScript, use BigInt for large values. - By hand: long division gives the quotient and remainder directly; the Long Division Calculator shows the working.
For common factors, which decide whether an inverse exists, use the GCF Calculator, and for divisibility questions the Prime Factorization Calculator is useful.
Common mistakes
- Assuming % means the same thing in every language. It does for positive numbers only.
- Using a negative remainder as an array index. Normalise with
((i % n) + n) % n. - Computing a huge power before reducing. Reduce after every multiplication.
- Expecting an inverse to always exist. Check that the GCD of a and m is 1 first.
- Dividing in modular arithmetic. There is no ordinary division; you multiply by the modular inverse instead.
Frequently asked questions
What is 17 mod 5?
2, because 17 = 5 × 3 + 2. All conventions agree when both numbers are positive.
Why does −17 mod 5 give 3 in Python but −2 in JavaScript?
Python's % uses floored division, so the remainder takes the sign of the divisor. JavaScript's % truncates towards zero, so the remainder takes the sign of the dividend.
What is the difference between remainder and modulo?
For positive numbers they are the same. For negatives, "remainder" usually means the truncated result and "modulo" the floored or Euclidean result, though usage varies between languages.
What is x mod 1?
For whole numbers it is always 0. For decimals it returns the fractional part in the floored convention: 5.75 mod 1 = 0.75.
When does a modular inverse not exist?
When the number and the modulus share a common factor greater than 1. For example, 4 has no inverse modulo 26 because both are divisible by 2.