Skip to content
Math & Statisticsnumber theory

Modulo Calculator

The modulo of two integers under both conventions, because they disagree on negative numbers and the disagreement causes real bugs. Minus seventeen mod five is three in Python and minus two in C, and both are defensible.

Also called: mod calculator, remainder calculator.

Result
3

-17 mod 5 is 3 by the mathematical convention, and -2 by the one most programming languages use. The two differ because the division rounds differently: flooring gives 3, which takes the sign of the divisor and is what Python returns, while truncating gives -2, which takes the sign of the dividend and is what C, Java, JavaScript and Go return. To get the mathematical result in those languages, use ((a % n) + n) % n. Enter a power to see modular exponentiation, which is computed by repeated squaring rather than by raising and then reducing.

Remainder as most languages compute it
-2
Quotient, floor division
-4
Quotient, truncated division
-3
The dividend raised to the power, mod the divisor
0
Congruence class
-17 is congruent to 3 modulo 5
On the sign
The two differ because the division rounds differently: flooring gives 3, which takes the sign of the divisor and is what Python returns, while truncating gives -2, which takes the sign of the dividend and is what C, Java, JavaScript and Go return. To get the mathematical result in those languages, use ((a % n) + n) % n.
On modular exponentiation
Enter a power to see modular exponentiation, which is computed by repeated squaring rather than by raising and then reducing.

The cycle of remainders

DividendFloored, as in PythonTruncated, as in C
-323-2
-273-2
-223-2
-173-2
-123-2
-73-2
-23-2
Method and background

How this is calculated

Modulo is the remainder after division, and everything hinges on how the division rounds. Flooring the quotient toward negative infinity gives a result that always takes the sign of the divisor, which is the mathematical convention and what Python does. Truncating the quotient toward zero gives a result taking the sign of the dividend, which is what C, Java, JavaScript and Go do. For positive inputs they agree exactly, which is why the difference goes unnoticed until a negative index or a wrapped timestamp appears. Modular exponentiation is computed by repeated squaring rather than by raising and then reducing, since the intermediate value would overflow long before the answer.

flooring the quotient makes the result take the sign of the divisor, which is what the mathematical convention wants and what most languages do not do
a
Dividend
n
Divisor

Worked examples

Each of these is asserted on every build. If a change to the engine ever moved one of these answers, the build would fail before the page could print it.

a negative dividend splits the conventions

Dividend
-17
Divisor
5
Raise the dividend to this power first
0

Result3

floor(-3.4) is -4 while trunc is -3, which is the whole disagreement

Open this example

positive inputs agree

Dividend
17
Divisor
5
Raise the dividend to this power first
0

Result2

boundary: the conventions only diverge on negatives

Open this example

Method and limits

What it assumes

  • Integer inputs, since modulo of a real number is a different operation.

What it deliberately does not model

  • A divisor of zero has no remainder, and this page declines rather than returning anything.

Formula version 1.0.0 · definition 1.0.0 · United States · Report a problem with this calculator

Frequently asked questions

Why do languages disagree about negative modulo?
Because they disagree about how division rounds. Flooring gives the divisor sign and truncating gives the dividend sign. Both are consistent; they are just not the same.
How do I make a negative index wrap correctly?
Add the divisor and take the modulo again. In a truncating language, ((a % n) + n) % n gives the mathematical result for any sign of a.