Skip to content
hateitall

Modulo Calculator

Get the right mod for your language or textbook, especially when a number is negative.

Processed locallyIn: NumberOut: Number, Table

Loading tool…

How to use modulo calculator

  1. Enter the dividend a and the divisor n.

  2. Read the floored result, the convention used in maths and Python.

  3. Compare the truncated and Euclidean results in the table, with the languages that use each.

  4. Switch to Power mod to compute b^e mod m with square and multiply steps.

What this tool does

  • Three conventions side by side with their quotients: truncated (JavaScript, C, Java), floored (Python, Excel) and Euclidean.
  • A table of the same sizes with every combination of signs.
  • Exact for whole numbers of any size and for decimals and fractions.
  • Modular exponentiation of huge numbers, with the square and multiply steps for small exponents.

How the result is verified

Every row satisfies a = n x q + r, which you can check from the quotient and result shown. With two positive numbers all three conventions agree, which is a quick sanity test.

Frequently asked questions

Why does -17 % 5 give -2 in JavaScript but 3 in Python?

JavaScript's % is a remainder: it rounds the quotient toward zero (-3), so the result takes the sign of -17. Python's % floors the quotient (-4), so the result takes the sign of 5. Both satisfy -17 = 5 x q + r; they just pick a different q.

How do I get a positive mod in JavaScript?

Use ((a % n) + n) % n for a positive n. That gives the floored and Euclidean answer, which is what you want for wrapping array indexes, hours on a clock or angles.

Is my data uploaded to a server?

No. Everything you drop, paste or type is processed by code running in this browser tab. You can check it yourself: open your browser's developer tools, watch the Network panel, and use the tool. No request carries your data.

Greatest common divisor and least common multiple with Euclid's steps

Whole numbers→ NumberLight on device

Primality test for huge numbers, prime factorization and nearest primes

Whole number→ VerdictModerate on device

Trig, logs, powers and factorials with memory, history and keyboard input

Expression→ NumberLight on device

Binary, octal, decimal, hex and any base 2 to 36, any length

Number→ NumberLight on device