How to use it
Enter the dividend a and the divisor n. The main answer uses the floored convention, and a table below gives the remainder under all three conventions in common use. Decimals are allowed, and n may be negative but not zero.
The formula
floored: q = ⌊a ÷ n⌋, so r has the sign of n
truncated: q = a ÷ n rounded toward 0, so r has the sign of a
Euclidean: q chosen so that 0 ≤ r < |n|a — dividend; n — divisor (not 0); q — whole-number quotient; r — remainder
When a and n are both positive, all three give the same remainder. They differ only when a negative number is involved, and the difference is one of definition, not accuracy.
| Convention | Remainder takes the sign of | Used by |
|---|---|---|
| Floored | the divisor n | Python %, Excel and Google Sheets MOD |
| Truncated | the dividend a | JavaScript, C, C++, Java and C# % |
| Euclidean | neither — never negative | Number theory; functions such as rem_euclid in some languages |
For a positive divisor, floored and Euclidean agree: the remainder lies from 0 up to, but not including, n — the answer mathematics textbooks use.
Worked examples
−17 mod 5
Given: a = −17, n = 5
- Floored: −17 ÷ 5 = −3.4, rounded down to q = −4, so r = −17 − (−4 × 5) = 3.
- Truncated: −3.4 rounded toward zero is q = −3, so r = −17 − (−3 × 5) = −2.
- Euclidean: the remainder must be at least 0, so r = 3.
That is why -17 % 5 returns 3 in Python but −2 in JavaScript. Both are correct under their own definition.
A negative divisor: 17 mod −5
Given: a = 17, n = −5
17 ÷ −5 = −3.4. Floored: q = −4 and r = 17 − (−4 × −5) = −3, negative like the divisor. Truncated: q = −3 and r = 2. Euclidean: r = 2.
Decimals: 7.5 mod 2 and 5.5 mod 0.1
Given: 7.5 mod 2; 5.5 mod 0.1
7.5 = 3 × 2 + 1.5, so the remainder is 1.5. For 5.5 mod 0.1 the calculator returns exactly 0, because 5.5 = 55 × 0.1. Ordinary floating-point arithmetic gets this one wrong: in JavaScript, 5.5 % 0.1 evaluates to about 0.0999999999999997, because 0.1 has no exact binary representation.
How the calculator works
- Both inputs are scaled to exact integers before dividing (5.5 and 0.1 become 55 tenths and 1 tenth), so decimal remainders carry no rounding error.
- The quotient shown with the answer is the floored quotient ⌊a ÷ n⌋.
- Modulo by zero is undefined and is rejected.
Where modulo turns up
Clock arithmetic is modulo 12: 10 o’clock plus 5 hours is (10 + 5) mod 12 = 3 o’clock. Weekdays repeat modulo 7: 100 days after a Monday is a Wednesday, because 100 mod 7 = 2. A number is even exactly when n mod 2 = 0. For weekday questions on real calendar dates, the date calculator does the counting for you.
Questions
Is modulo the same as the remainder?
For positive numbers, yes. With negative numbers the word “remainder” often refers to the truncated result (C calls % the remainder operator), while “modulo” usually means the floored or Euclidean result. Usage is not consistent across languages, so check the documentation of the one you use.
Why does Excel’s MOD(−17, 5) give 3?
Excel computes MOD(n, d) as n − d × INT(n ÷ d), and INT rounds down. That is the floored convention, so the result takes the sign of the divisor: −17 − 5 × (−4) = 3.
What does x mod 1 give?
The fractional part, measured upward from the integer below: 7.25 mod 1 = 0.25 and, in the floored convention, −7.25 mod 1 = 0.75.
Method & sources
- The truncated, floored and Euclidean definitions, and which languages use each — Wikipedia: Modulo