Modulo Calculator

Compute quotient, remainder, GCD, and modular exponentiation with three sign conventions and BigInt precision.

Truncated
C, C++, JS, Java
Floored
Python, Ruby
Euclidean
Math mod, always ≥ 0
Divisibility
GCD(a, b)
Step-by-step explanation

Enter values above to see the breakdown.

Three modulo conventions, explained

When the dividend a is negative, different languages disagree on what a mod b should return. All three results below are correct — they just use different rules.

Truncated (C, C++, JS, Java)

Uses q = trunc(a/b), rounding toward zero. The remainder takes the sign of the dividend. Example: -17 % 5 = -2.

Floored (Python, Ruby)

Uses q = floor(a/b), rounding toward negative infinity. The remainder takes the sign of the divisor. Example: -17 % 5 = 3.

Euclidean (mathematical mod)

Always returns a non-negative remainder in the range [0, |b|). This is the convention used in number theory. Example: -17 mod 5 = 3.

Frequently asked questions

Why do different languages give different answers for negative numbers?
The modulo operator is defined by a = b*q + r, but the standard leaves the sign of r up to the language. C, C++, Java, and JavaScript truncate the quotient toward zero, so the remainder keeps the sign of a. Python and Ruby floor the quotient toward negative infinity, so the remainder keeps the sign of b. Both satisfy the equation — they just pick different quotients.
Which convention should I use?
For pure math, number theory, or when you need a result in [0, b), use the Euclidean (mathematical) mod. For array indexing with wrap-around (like a circular buffer) you usually want the floored convention, because it handles negatives gracefully. If you are implementing a language runtime or matching C behavior exactly, use truncated.
What is divisibility and how does this calculator check it?
a is divisible by b when a mod b = 0 — the remainder is zero. This calculator flags divisibility as soon as the Euclidean remainder is 0. For example, 100 is divisible by 5 (100 mod 5 = 0) but not by 7 (100 mod 7 = 2).
Where is modulo actually used in the real world?
Everywhere. Hash tables distribute keys across buckets with hash(key) mod N. Cryptography (RSA, Diffie–Hellman, elliptic curves) relies on modular arithmetic. Time and date math (day of week, clock) uses mod 7, mod 24, mod 60. Checksums like ISBN and Luhn use mod 10 or mod 11. Pseudorandom generators use mod 2^32. Pagination: (page - 1) mod pageSize.
Does this calculator handle very large numbers?
Yes. Inputs are parsed as JavaScript BigInt, so there is no 253 precision limit. You can paste a 200-digit number and get an exact result. The modular exponentiation tab uses fast square-and-multiply with BigInt, so expressions like 123456789987 mod (109+7) return instantly.
What is modular exponentiation and why is it efficient?
Modular exponentiation computes (baseexponent) mod m without ever building the huge intermediate power. The algorithm squares the base and reduces mod m at each step, using the binary expansion of the exponent. This runs in O(log exponent) BigInt multiplications — fast enough for RSA keys with 2048-bit exponents. A naive loop that multiplies exponent times and then takes mod would take billions of years on the same input.

For educational and development use. Results are exact for integer inputs within BigInt range.

The Modulo Calculator computes a mod b for integers of any size, showing three remainder conventions side by side: truncated (C, JavaScript, Java), floored (Python, Ruby), and Euclidean (mathematical mod, always in the range 0 to |b|-1). It also displays the full equation a = b * q + r, checks whether b divides a, and returns the greatest common divisor. A step-by-step breakdown explains how each convention derives its result, which is useful when porting code between languages. Example: for -17 and 5, the truncated result is -2, the floored result is 3, and the Euclidean result is 3. A clock-math tab visualises mod 12 and mod 24 wrap-around on a 24-hour clock face. A modular exponentiation tab computes (base^exponent) mod m with BigInt fast exponentiation in O(log exponent) time, suitable for cryptography (RSA, Diffie-Hellman) and primality tests.