How does modulo work, even with negative numbers?
Remainders under every sign convention, modular powers and inverses, on a live clock face.
What you enter stays on your device while you use this tool. Opening a shared link sends the settings in its address to HowToolbox to prepare the page.
−31 mod 12
5
−31 = −3 × 12 + 5: the remainder is what is left after taking away whole multiples of 12.
Key values as a table
| Sign rule | q | r |
|---|---|---|
| Never negative (maths) | −3 | 5 |
| Sign of n (Python, Excel) | −3 | 5 |
| Sign of a (JavaScript, C, Java) | −2 | −7 |
- Quotient q
- −3
- Congruence
- −31 ≡ 5 (mod 12)
- Never negative (maths)
- 5
- Sign of n (Python, Excel)
- 5
- Sign of a (JavaScript, C, Java)
- −7
Remainders under every sign convention, modular powers and inverses, on a live clock face.
How to use
- Choose a mod n, a modular power aᵇ mod n or a modular inverse a⁻¹ mod n.
- Type the whole numbers, or drag the point round the clock face to change a.
- For a negative number, pick the sign rule you need; the answers of maths, Python and JavaScript are listed side by side.
How it works
a mod n is the remainder r after taking away whole multiples of n: a = q × n + r. Think of a clock with n numbers, 0 at the top: start at 0 and take a steps (backwards for a negative a); r is where you land. 31 hours before midnight a 12-hour clock shows 5, because −31 = −3 × 12 + 5.
For negative numbers the rules differ in which name they give that spot. Maths uses the Euclidean remainder, never negative (0 ≤ r < |n|). Python and Excel round q down, so r takes the sign of n. JavaScript's %, C and Java round q toward 0, so r takes the sign of a: −31 % 12 is −7 there. The answers differ by n and are congruent: −7 ≡ 5 (mod 12).
Modular powers are found by repeated squaring: 4¹³ mod 497 = 445, without ever writing out 4¹³ in full. The inverse of a is the x with a × x ≡ 1 (mod n), found by the extended Euclidean algorithm: 3 × 4 = 12 ≡ 1 (mod 11), so 3⁻¹ mod 11 = 4. It exists only when a and n have no common factor. Sources: Khan Academy, modular arithmetic; OpenStax Contemporary Mathematics, section 3.7.
Worked example
What is −31 mod 12?
- Write −31 = q × 12 + r. Maths takes q = −3, so r = −31 − (−3 × 12) = 5.
- JavaScript and C round the quotient toward 0 instead: q = −2, so the remainder is −7.
5 in maths and Python, −7 in JavaScript; the two differ by 12.
Sources
- Khan Academy: What is modular arithmetic?Used for: definitions
Frequently asked questions
Why does −7 % 3 give −1 in JavaScript but 2 in Python?
JavaScript rounds the quotient toward 0 (−2), so the remainder is −7 − (−6) = −1. Python rounds down (−3), so it is −7 − (−9) = 2. Both are right under their own rule.
How do I always get a non-negative remainder in code?
Use ((a % n) + n) % n for a positive n. It turns JavaScript's or C's truncated remainder into the Euclidean one.
Where is modular arithmetic used?
Clocks and calendars, check digits such as IBAN and ISBN, hash tables, and public-key cryptography such as RSA, which relies on modular powers and inverses.
Why is there no inverse of 4 mod 6?
4 and 6 share the factor 2, so every multiple of 4 is even modulo 6 and can never be 1.
What is the difference between mod and remainder?
For positive numbers there is none. For negative numbers "mod" usually gives a result in 0 to n − 1 (or the sign of n), while a "remainder" such as JavaScript's % takes the sign of a.
Next you might need
More in Math & numbers
- Percentage calculator, private, runs on your device
- Area of shapes, private, runs on your device
- Combinations and permutations calculator, private, runs on your device
- Complex number calculator, private, runs on your device
- Confidence interval calculator, private, runs on your device
- Correlation and regression calculator, private, runs on your device





