Modulo Calculator
Type a dividend and a divisor and both answers arrive together, because for a negative operand there genuinely are two. -7 mod 3 is 2 to a mathematician and to Python, and -1 in C, C++, C#, Java, JavaScript, Go, Rust and Swift, and a page that prints one of those and calls it the answer sends half its visitors away with the wrong number. Here the Euclidean modulo, the truncated remainder, the matching quotient and the congruence are all in the readout at once: -7 and 3 gives 2 and -1, with a quotient of -3, since -7 = 3 x (-3) + 2. The floored convention sits beside them as a third answer rather than a synonym, because it agrees with Euclidean for every positive divisor and can part from it once the divisor is negative: 7 mod -3 is -2 in Python and Excel and 1 under Euclidean. A table under the result names the operators and functions that return each one, worked out for the numbers you typed rather than for an example. Everything is exact integer arithmetic, so a 1,000-digit dividend, or 2^64 written with a caret, is divided without being rounded to a float: 2^64 mod 97 is 61. Nothing is uploaded, nothing needs an account, and the only thing kept is your last entry in this browser.
-7 mod 3 is 2 under the Euclidean convention and -1 as a truncated remainder. The Euclidean modulo answer is 2, with a quotient of -3, since -7 = 3 x (-3) + 2.
Exact integer arithmetic on a 1-digit dividend and a 1-digit divisor: dividend = divisor x quotient + remainder holds exactly on every line below.
Both answers are worked out every time: the buttons only choose which one takes the big type, and the other stays in the readout beside it. Each box takes a whole number of up to 1,000 digits, and spaces, commas and underscores are read as group separators, so 1,000 and 1 000 are the same number. A caret or double-star power is expanded exactly before the division, so 2^64 is its twenty digits rather than a rounded float. A divisor of 0 is refused, because no remainder exists to show there. The congruence is written with the smallest remainder that is not negative and with the size of the divisor, which is how congruence notation is normally read. Your entry is kept in this browser so the page opens where you left it.
What each language returns
The same division under all three conventions, worked out for the two numbers above. Every row satisfies dividend = divisor x quotient + remainder; only the rounding of the quotient differs.
| Convention | Where you get it | Quotient | Result |
|---|---|---|---|
| Truncated remainderThe remainder takes the sign of the dividend. | % in C, C++, C#, Java, JavaScript, Go, Rust, Swift and PHP; rem in Haskell and Ada | -2 | -1-7 = 3 x (-2) + (-1) |
| Floored moduloThe remainder takes the sign of the divisor. | % in Python, Ruby and Perl; mod in Haskell; Math.floorMod in Java; MOD in Excel | -3 | 2-7 = 3 x (-3) + 2 |
| Euclidean moduloThe remainder is never negative. | % in Dart; rem_euclid in Rust; mod in ISO Pascal | -3 | 2-7 = 3 x (-3) + 2 |
Common questions
- What is -7 mod 3?
- It is 2, or -1, and which one is right depends on the convention you are working in. Mathematicians and Python give 2: the Euclidean modulo is never negative, and -7 = 3 x (-3) + 2 with the quotient rounded down to -3. C, C++, C#, Java, JavaScript, Go, Rust, Swift and PHP give -1: their % operator truncates the quotient toward zero, to -2, and the remainder is left with the sign of the dividend, so -7 = 3 x (-2) + (-1). Both identities are exactly true, which is why this page shows both figures side by side and labels which is which instead of picking one.
- Why do Python and JavaScript give different answers for the same modulo?
- Because they round the quotient in different directions, and the remainder is whatever is left after that. Python floors the quotient toward minus infinity, so -7 // 3 is -3 and -7 % 3 is 2. JavaScript truncates toward zero, so the same division gives -2 and the remainder is -1. Neither is a bug: both satisfy dividend = divisor x quotient + remainder. The rule of thumb is that a truncated remainder carries the sign of the dividend and a floored one carries the sign of the divisor, and for two positive numbers the two rules never disagree, which is why the difference only shows up once a minus sign appears.
- What is the difference between the remainder and the modulo?
- In everyday use they are the same thing, and for positive numbers they always agree: 17 and 5 gives 2 either way. They separate on negative inputs. Remainder normally means the result of the truncating division a processor performs, which keeps the sign of the dividend. Modulo, in number theory, means the representative of a congruence class, which is taken to be the smallest value that is not negative. This page labels the Euclidean modulo and the truncated remainder separately, prints the quotient that goes with whichever one you put in the big type, and states the congruence, so you can quote the one your context expects.
- What happens when the divisor is negative?
- The three conventions split three ways, which is exactly when a single-answer calculator becomes misleading. Take 7 and -3. The truncated remainder is 1, because it follows the sign of the dividend. The floored result, which is what Python and Excel return, is -2, because it follows the sign of the divisor. The Euclidean modulo is 1, because it is never negative. Take -7 and -3 and the truncated and floored answers are both -1 while the Euclidean modulo is 2. All three are shown at once here, each with its own quotient and its own identity, so the one you need is never hidden behind the one the page preferred.
- Can it work out 2^64 mod 97 exactly?
- Yes, and the answer is 61. The arithmetic runs on arbitrary-precision integers from the moment the number is read, so nothing passes through a floating-point value that would lose the low bits: 2^64 is 18446744073709551616 exactly, the quotient is 190172619316593315, and 97 x 190172619316593315 + 61 puts you back at the number you started with. You can type the digits out or write 2^64 with a caret and the power is expanded before the division. Each box takes up to 1,000 digits, and anything past that is refused with the count rather than quietly cut.
- Why does a divisor of 0 return an error instead of a number?
- Because there is no number to return. Every convention here is built on dividend = divisor x quotient + remainder, and with a divisor of 0 that equation either has no solution or has infinitely many, so no remainder is defined. Programming languages differ on what they do about it: C leaves integer division by zero undefined, Java throws an ArithmeticException, and JavaScript quietly hands back NaN. Showing NaN would look like an answer, so this page refuses the division and says which box to change.
- How do you work out a modulo by hand?
- Divide, throw the fractional part away in the direction your convention says, then multiply back and subtract. For 17 and 5: 5 goes into 17 three times, so the quotient is 3, and 17 minus 5 x 3 leaves a remainder of 2. For a negative dividend the only decision is which way to round. -7 divided by 3 is -2.33, so truncating gives -2 and 3 x (-2) is -6, leaving -1; rounding down gives -3 and 3 x (-3) is -9, leaving 2. The page prints that final line, the identity, beside every result, so you can check your own working against it rather than trusting the number alone.
Exact integer arithmetic through BigInt, so a three hundred digit number is not rounded or approximated. Both conventions are printed because they genuinely disagree on negative inputs: the truncated remainder takes the sign of the dividend, the Euclidean modulo is never negative. A zero divisor is refused rather than returned as infinity.