CalculateItNow home

Modulo Calculator

a mod b with the quotient and remainder, showing where the mathematical modulo and the % operator in C, Java and JavaScript disagree on negative numbers.

Modulo Calculator: with the default inputs, a mod b (mathematical) is 2.

Try an example
a mod b (mathematical)
2

The floored modulo — takes the sign of b. This is what Python's % returns.

a % b (C, Java, JavaScript)
2
Floored quotient ⌊a ÷ b⌋
3
Truncated quotient
3
Euclidean remainder
2
Congruence
17 ≡ 2 (mod 5)
Assumptions
  • The primary result is the floored (mathematical) modulo; the truncated % result is reported alongside it.
  • Integer inputs are computed exactly; non-integer inputs inherit binary floating-point rounding.
  • b = 0 is rejected rather than returned as NaN or Infinity.
How the answer moves as a changes
aa mod b (floored)a % b (truncated)⌊a ÷ b⌋
12222
13332
14442
15003
16113
17223
18333
19443
20004
21114
22224

The floored modulo cycles smoothly through the same values forever, including across zero. The truncated remainder jumps sign at zero, which is exactly why the two disagree for negative dividends.

Math verified by automated testsUpdated 2026-09-092 sources cited

How this is worked out

The formula

Floored (mathematical) modulo:   a mod b = a − b⌊a ÷ b⌋      sign follows b
Truncated remainder (the % operator):  a % b = a − b·trunc(a ÷ b)   sign follows a
Euclidean remainder:  always 0 ≤ r < |b|

All three satisfy  a = b × quotient + remainder — they differ only in how the
quotient is rounded, and therefore only when a and b have opposite signs.

Open How it’s calculated above to see this worked through with your own numbers.

What you enter

Dividend (a)
The number being divided. Negative values are where the conventions differ — try −17.defaults to 17
Divisor (b) — the modulus
Cannot be 0.defaults to 5

What you get back

a mod b (mathematical)main answer
The floored modulo — takes the sign of b. This is what Python's % returns.
a % b (C, Java, JavaScript)
The truncated remainder — takes the sign of a.
Floored quotient ⌊a ÷ b⌋
Truncated quotient
Euclidean remainder
Always zero or positive, whatever the signs.
Congruence

What this assumes

  • The primary result is the floored (mathematical) modulo; the truncated % result is reported alongside it.
  • Integer inputs are computed exactly; non-integer inputs inherit binary floating-point rounding.
  • b = 0 is rejected rather than returned as NaN or Infinity.

About this calculator

Modulo is what is left over after division. 17 divided by 5 is 3 with 2 left over, so 17 mod 5 = 2. That much everyone agrees on. The moment a negative number appears, programming languages split into two camps — and this calculator shows both answers side by side so you can see which one your tool uses.

The two conventions

Every definition of remainder satisfies the same identity: a = b × quotient + remainder. What differs is how the quotient is rounded.

  • Floored (mathematical) modulo rounds the quotient down, towards negative infinity. The remainder then always takes the sign of the divisor. This is what mathematicians mean by "mod", what number theory is built on, and what Python, Ruby and Excel's MOD return. −17 mod 5 = 3.
  • Truncated remainder rounds the quotient towards zero. The remainder then takes the sign of the dividend. This is what the % operator does in C, C++, Java, C#, JavaScript, Go and Rust. −17 % 5 = −2.

Both are correct; they answer slightly different questions. They agree exactly when a and b have the same sign, which is why most people never notice the difference until a bug involving a negative index or a wrapped clock time appears.

A third convention, Euclidean, insists the remainder is never negative, whatever the signs. For a positive divisor it matches the floored answer.

Why this bites in real code

The classic bug is wrapping an index around a circular buffer or an array. Writing i % n looks right until i goes negative, at which point you get a negative index instead of one near the top of the range. The portable fix is ((i % n) + n) % n, which forces the floored answer in any language.

The same thing happens with clock arithmetic (3 hours before 1 o'clock), with angles that should wrap into 0–360°, and with hash buckets when the hash is a signed integer.

Other things modulo is good for

  • Divisibility. a mod b = 0 exactly when b divides a. n % 2 tests odd or even.
  • Cycles. Days of the week, months, and any repeating pattern of length n are naturally indexed mod n.
  • Cryptography and checksums. RSA, Diffie-Hellman, ISBN check digits and the Luhn algorithm are all modular arithmetic.

Edge cases

b = 0 is undefined and the calculator refuses it rather than returning NaN — you cannot split a into groups of zero. Non-integer inputs are allowed and use the same formulas, but binary floating point means the answer can be off in the final digits; for exact fractional work, scale to integers first.

Frequently asked questions

What is the modulo operator?

It returns the remainder after dividing one number by another. 17 mod 5 is 2, because 17 is three fives with 2 left over.

Why is −17 mod 5 equal to 3 in Python but −2 in JavaScript?

Python floors the quotient, so the remainder takes the sign of the divisor. C-family languages truncate towards zero, so the remainder takes the sign of the dividend. Both satisfy a = b×q + r; they round q differently.

How do I always get a positive remainder?

Use ((a % b) + b) % b. That expression gives the floored, non-negative answer in any language that has a % operator.

What is a mod 0?

Undefined. Integer division by zero throws in most languages, and the floating-point version returns NaN. This calculator rejects it with a message instead.

Is modulo the same as remainder?

In everyday use, yes. Strictly, 'modulo' usually means the floored version and 'remainder' the truncated one, which is precisely the distinction that shows up with negative numbers.

Put this calculator on your own site

A working modulo, free for any site, with no ads and no sign-up. It resizes to fit wherever you paste it and updates itself as this page improves.

Paste this anywhere. It works on any site, carries no ads, never expires, and always shows the current version.

Modulo Calculator by CalculateItNow

The page's own title. The clearest description of what the link leads to.

The credit line sits outside the widget on purpose, so it is a real link on your page rather than one buried in a frame. Please keep it — it is what pays for CalculateItNow staying free and ad-free. The script only resizes the widget to fit its contents; drop it and the widget still works.

Browse every calculator widget·How to add it to WordPress, Squarespace or Wix

The questions people ask next to a modulo.

All math calculators·Browse everything