Modulo Calculator
Compute a mod n with the correct sign for negative numbers, check a congruence a ≡ b (mod n), find a modular inverse, or compute a large modular exponentiation aᵇ mod n exactly — every step shown.
Written by Suhaib Hassan. Every worked example is re-derived by hand. Our methodology.
Modulo uses exact BigInt division; the modular inverse uses the extended Euclidean algorithm; exponentiation uses fast (binary) modular exponentiation, so even huge exponents stay exact and fast.
Programming languages disagree on the sign of a % b for negative numbers — this shows both conventions side by side.
—
Where a lands on the number line mod n
Every multiple of n is marked; a wraps to its residue in [0, n).
Step-by-step working
—
Floored vs. truncated modulo
Languages disagree on the sign of the result when a is negative.
| Convention | Formula | −7 mod 3 | Used by |
|---|---|---|---|
| Floored (this calculator's default) | a − n·⌊a/n⌋ | 2 | Python %, mathematics |
| Truncated | a − n·trunc(a/n) | −1 | C, Java, JavaScript % |
Worked examples
Negative dividend
What is −7 mod 3?
−7 mod 3 = 2 (floored)
Congruence
Is 17 ≡ 5 (mod 6)?
Yes — both sides give the same remainder
Modular inverse
Find 3⁻¹ mod 11.
3⁻¹ ≡ 4 (mod 11)
Key definitions
- Modulo: a mod n is the remainder left after dividing a by n, with 0 ≤ (a mod n) < n for n > 0 under the floored convention.
- Congruence: a ≡ b (mod n) means n divides (a − b) exactly — a and b leave the same remainder.
- Modular inverse: a⁻¹ mod n is the value x with a·x ≡ 1 (mod n); it exists exactly when gcd(a, n) = 1.
- Modular exponentiation: aᵇ mod n, computed by repeated squaring so it stays fast even for huge b.
By Suhaib Hassan · Reviewed 29 September 2026 · How CalculatePilot verifies formulas →
Sources
- Gauss, C. F. (1801). Disquisitiones Arithmeticae, Article 1 (the definition of congruence).
- Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2 (3rd ed.), section 4.5.2 (the Euclidean algorithm and modular inverses).
- ISO/IEC 80000-2:2019, clause 9 (the floor function and integer division).
Frequently asked questions
Why is −7 mod 3 equal to 2, not −1?
Under the mathematical (floored) convention, the result of a mod n always has the same sign as n (here positive), so it stays in [0, n). −7 = 3 × (−3) + 2, giving remainder 2. Some programming languages instead truncate toward zero, giving −1 — this calculator shows both.
When does a modular inverse not exist?
Exactly when gcd(a, n) ≠ 1 — that is, a and n share a common factor. For example, 4 has no inverse mod 8, since gcd(4, 8) = 4.
How is aᵇ mod n computed for a huge exponent b?
By fast (binary) exponentiation: repeatedly squaring a mod n and multiplying in the factors corresponding to the binary digits of b, taking the modulus at every step. This needs only about log₂(b) multiplications, so even a 1000-digit exponent computes instantly.
What is the modulo operation used for?
Clock and calendar arithmetic, hash tables, checksums, cryptography (RSA relies on modular exponentiation), pseudo-random number generators, and cycling through array indices.
Is a mod 0 defined?
No — division by zero is undefined, so a mod 0 has no value. This calculator flags it as an error rather than returning a result.
How is congruence different from equality?
Equality means two numbers are identical. Congruence mod n is weaker: it only requires them to leave the same remainder when divided by n, so infinitely many numbers (…, b−n, b, b+n, b+2n, …) are all congruent to b.
Related calculators
Continue with number theory.