The modulo operation, written a mod n (or a % n in code), gives the remainder left when a is divided by n. It’s the arithmetic of clocks and calendars, of checksums and hash tables, of “every third row” and “odd or even”. With positive numbers everyone agrees on the answer. With negatives — or with decimals — calculators, spreadsheets and programming languages quietly disagree. This tool shows the result under all four common conventions so you can match the one you need.
How to use the modulo calculator
- Enter the dividend a — the number being divided.
- Enter the divisor n — the modulus. It can be negative or a decimal, but not zero.
- Choose the convention you want for the headline answer. The table underneath lists all four.
- For small whole-number moduli, a clock diagram shows where a lands when you count around n positions.
The definition
Every convention finds a quotient q and remainder r with:
They differ only in how q is rounded:
| Convention | Quotient q | Sign of r | −17 mod 5 |
|---|---|---|---|
| Floored | ⌊a ÷ n⌋, rounded down | Same as n | 3 |
| Truncated | a ÷ n with decimals dropped | Same as a | −2 |
| Euclidean | Chosen so r ≥ 0 | Never negative | 3 |
| Rounded (IEEE) | a ÷ n to the nearest integer | Either | −2 |
Worked example: −17 mod 5
Divide: −17 ÷ 5 = −3.4
Floored: round down to q = −4; r = −17 − 5 × (−4) = 3
Truncated: drop the decimals, q = −3; r = −17 − 5 × (−3) = −2
Check both: 5 × (−4) + 3 = −17 and 5 × (−3) + (−2) = −17
The two answers differ by exactly 5, the modulus. That is always the case when the floored and truncated results disagree.
Which convention does my language use?
| Language or tool | Operator | −17 mod 5 |
|---|---|---|
| Python | -17 % 5 |
3 |
| Excel, Google Sheets | =MOD(-17, 5) |
3 |
| JavaScript, TypeScript | -17 % 5 |
−2 |
| C, C++, Java, C#, Go | -17 % 5 |
−2 |
| Rust | -17 % 5 / (-17i32).rem_euclid(5) |
−2 / 3 |
| IEEE 754 remainder | remainder(-17, 5) |
−2 |
A common bug: using % in JavaScript to wrap an index around an array of length 5 gives −2 for −17, which is not a valid index. The fix is ((a % n) + n) % n, which turns any truncated result into the floored one for positive n.
Modular arithmetic in everyday life
- Clocks: 9 a.m. plus 50 hours is 9 + 50 = 59, and 59 mod 24 = 11 — so 11 a.m., two days later.
- Days of the week: 100 days from a Monday is 100 mod 7 = 2 days later, a Wednesday.
- Even and odd: n mod 2 is 0 for even numbers and 1 for odd ones.
- Check digits: ISBN-10 and many ID numbers use mod 11 or mod 10 sums to catch typing errors.
- Cryptography: RSA and similar systems do almost all their work mod a huge number; the modular inverse comes from the Euclidean algorithm.
Decimals and exactness
Binary floating point can’t store 0.1 exactly, so in many languages 0.3 % 0.1 returns about 0.09999999999999998 instead of 0. This calculator scales decimals to whole numbers before dividing, so the remainder is exact. For the quotient and remainder of ordinary long division with all the steps written out, see the long division calculator.
Frequently asked questions
What does mod mean?
a mod n is the remainder after dividing a by n. 17 mod 5 = 2 because 17 = 3 × 5 + 2. It's the arithmetic of clocks: 17 hours after midnight on a 12-hour clock reads 5, because 17 mod 12 = 5.
What is −17 mod 5?
It depends on the convention. In mathematics, Python and Excel, −17 mod 5 = 3, because −17 = −4 × 5 + 3. In C, Java and JavaScript, −17 % 5 = −2, because those languages truncate the quotient to −3.
Why do programming languages give different answers for negative numbers?
They round the quotient differently. Truncated division rounds toward zero, so the remainder takes the sign of the dividend. Floored division rounds down, so the remainder takes the sign of the divisor. Both satisfy a = n × q + r; they just pick different q.
Can I use mod with decimals?
Yes. 5.5 mod 2 = 1.5, because 5.5 = 2 × 2 + 1.5. The calculator works in exact decimals, so values like 0.3 mod 0.1 correctly give 0 instead of a floating-point leftover.
What is the difference between mod and remainder?
For positive numbers they are the same. For negatives, "mod" usually means the floored or Euclidean result (never negative for a positive modulus), while "remainder" in programming often means the truncated result, which can be negative.