Modulo and Remainder Calculator
Enter the dividend and divisor. Both conventions are shown, so you can see why -7 mod 3 is 2 in Python but -1 in JavaScript.
Remainder (a mod b)
2
Quotient
-3
Working
q = floor(-7 ÷ 3) = floor(-2.333333) = -3; r = -7 − 3 × (-3) = 2.
Other convention
Under the truncated convention the quotient is -2 and the remainder -1.
How it works
Dividing a by b gives a whole-number quotient q and a remainder r such that a = b × q + r. For positive numbers there is only one sensible answer: 17 ÷ 5 is 3 remainder 2. The remainder is what "17 mod 5" means, and it is the quantity behind clock arithmetic, day-of-the-week calculations, and checking whether a number is divisible by another.
With a negative dividend or divisor there are two reasonable choices of quotient, and languages and textbooks differ. The floored convention rounds the quotient down (toward minus infinity), so the remainder has the same sign as the divisor and, for a positive divisor, always lies in 0 to b − 1: -7 mod 3 = 2 because -7 = 3 × (-3) + 2. This is the mathematical definition and what Python's % and most spreadsheet MOD functions return.
The truncated convention rounds the quotient toward zero, so the remainder has the same sign as the dividend: -7 = 3 × (-2) − 1, giving -1. This is what the % operator returns in C, C++, Java, JavaScript and Go. Both conventions agree whenever a and b have the same sign, and the Other convention line always shows the alternative so you can match whichever tool you are checking against.
Formula
a = b × q + r floored: q = floor(a ÷ b), r = a − b × q (0 ≤ r < b for b > 0) truncated: q = trunc(a ÷ b), r = a − b × q (|r| < |b|, sign of r = sign of a)
Example
17 ÷ 5: the quotient is 3 and the remainder is 2 under both conventions, because 17 = 5 × 3 + 2.
-7 ÷ 3 with the floored convention: floor(-2.333) = -3, so r = -7 − 3 × (-3) = 2. With the truncated convention: trunc(-2.333) = -2, so r = -7 − 3 × (-2) = -1.
Decimals work too: 7.5 mod 2 is 1.5 with quotient 3, since 7.5 = 2 × 3 + 1.5.
Assumptions and limitations
- The quotient is always a whole number; the remainder may be a decimal when the inputs are.
- The division is treated as exact when the divisor times the nearest whole quotient comes within one billionth of the divisor of the dividend, and remainders are cleaned to 12 significant digits, so 1 mod 0.1 is reported as 0 rather than the 0.09999999999999995 that raw floating-point arithmetic gives.
- Only the floored and truncated conventions are shown. Euclidean division (remainder always non-negative) matches the floored result for a positive divisor and the truncated result for a positive dividend with a negative divisor.
Frequently asked questions
Why does -7 % 3 give -1 in JavaScript but 2 in Python?
JavaScript truncates the quotient toward zero (-2) and Python floors it (-3); the remainders that make a = b × q + r balance are -1 and 2 respectively. Both are valid remainders, they just come from different quotients. To get the mathematical (floored) result in JavaScript use ((a % b) + b) % b.
What is 27 mod 12, and why does it matter?
27 mod 12 = 3, because 27 = 12 × 2 + 3. That is the clock-arithmetic answer: 27 hours after midnight is 3 o'clock. Days of the week work the same way with mod 7.
More in Math & Geometry calculators.