Modulo Calculator
Calculate a mod n and the quotient for whole numbers, decimals and very large integers. Negative numbers are handled three ways (floored math mod, truncated % like C and JavaScript, and Euclidean) so you can match your math class or programming language.
How to use the Modulo Calculator
- Enter the number a you want to reduce.
- Enter the modulus n.
- Choose a sign convention: floored for math and Python, truncated for C, Java and JavaScript, or Euclidean for an always-positive result.
- Press Calculate to see the result, the quotient and the working.
Modulo formula
Every convention writes a = n × q + r and then computes r = a − n × q. They differ only in how the quotient q is rounded:
- Floored: q = ⌊a ÷ n⌋. The remainder has the sign of n.
- Truncated: q = trunc(a ÷ n), rounded toward zero. The remainder has the sign of a.
- Euclidean: q is chosen so that 0 ≤ r < |n|. The remainder is never negative.
| a, n | Floored | Truncated | Euclidean |
|---|---|---|---|
| 17, 5 | 2 | 2 | 2 |
| −17, 5 | 3 | −2 | 3 |
| 17, −5 | −3 | 2 | 2 |
| −17, −5 | −2 | −2 | 3 |
| 5.5, 2 | 1.5 | 1.5 | 1.5 |
Which languages use which rule
| Language or tool | Expression | Convention | −17 mod 5 |
|---|---|---|---|
| Python, Ruby | a % n | Floored | 3 |
| Excel, Google Sheets | MOD(a, n) | Floored | 3 |
| JavaScript, Java, C, C++, C#, Go | a % n | Truncated | −2 |
| Rust | a % n / a.rem_euclid(n) | Truncated / Euclidean | −2 / 3 |
| Python | math.fmod(a, n) | Truncated | −2.0 |
Frequently asked questions
What does mod mean?
a mod n is the remainder after dividing a by n. 17 mod 5 = 2 because 17 = 5 × 3 + 2. It is the basis of clock arithmetic: 50 hours after midnight the clock shows 50 mod 24 = 2.
What is −17 mod 5?
In mathematics (floored or Euclidean) it is 3, because −17 = 5 × (−4) + 3. In C, Java and JavaScript, −17 % 5 gives −2, because they round the quotient toward zero (−3) and the remainder takes the sign of the dividend.
Why do programming languages give different results for negative numbers?
They define the quotient differently. Python, Ruby and Excel's MOD round down (floored), so the result has the sign of the divisor. C, C++, Java, JavaScript, Go and Rust's % truncate toward zero, so the result has the sign of the dividend.
How do I get a positive modulo in JavaScript or C?
Use ((a % n) + n) % n. For a positive n this always gives a result from 0 to n − 1, the same as the floored and Euclidean conventions.
Can you use modulo with decimals?
Yes. 5.5 mod 2 = 1.5 because 5.5 = 2 × 2 + 1.5. This calculator works with exact decimal digits, so results like 0.3 mod 0.1 = 0 come out exactly instead of a tiny floating-point error.
What is the difference between modulo and remainder?
For positive numbers they are the same. With negatives, "remainder" usually means the truncated result (sign of the dividend) and "modulo" the floored or Euclidean result (never negative for a positive modulus).