Modulo Calculator
Modulo Calculator
Whole numbers in decimal, up to 1,000 digits each, worked out exactly. A minus sign (- or −) is fine, and so are commas or spaces between groups of three. a and m give the remainder in all three conventions, the GCD, Bézout coefficients and modular inverse; add b for ab mod m.
How to Use the Modulo Calculator
Enter a value and modulus
Type a whole number a and the modulus m. The headline remainder is the Euclidean one, from 0 to |m| − 1; a negative modulus is read as its absolute value.
Compare the three conventions
The table under the results shows the quotient and remainder under truncated, floored and Euclidean division, with the programming languages and spreadsheet functions that use each, so you can see why your code and this page might disagree.
Read the GCD, Bézout identity and inverse
The calculator shows the greatest common divisor, the integers x and y with a·x + m·y = gcd from the extended Euclidean algorithm, and the modular inverse when a and m are coprime.
Raise to a power
Add an exponent b to compute ab mod m by repeated squaring. The result says how many squarings and multiplications it took. A negative b is read as a power of the inverse.
One Division, Three Remainders
Why −7 mod 3 has more than one answer
Dividing a by m gives a quotient q and a remainder r with a = m·q + r. When a/m is not a whole number, q has to be rounded, and the remainder follows from that choice. Daan Leijen’s survey Division and Modulus for Computer Scientists sets out the options. Truncated division rounds q toward zero, so the remainder takes the sign of a: −7 ÷ 3 gives q = −2 and r = −1. Floored division rounds q down, so the remainder takes the sign of m: q = −3 and r = 2. Euclidean division keeps the remainder between 0 and |m| − 1 whatever the signs.
With a positive modulus, floored and Euclidean always agree, so a comparison that only tries a positive divisor sees just two answers. They part only when m is negative: 7 mod −3 is −2 floored and 1 Euclidean, and −7 mod −3 is −1 floored and 2 Euclidean. The table under the calculator shows all three for whatever you type, and names where you meet each one: JavaScript, C, Java, Go, Rust and PHP truncate with %; Python’s %, Excel’s MOD and Java’s Math.floorMod are floored; Rust’s rem_euclid is Euclidean. This calculator’s headline answer is the Euclidean one.
Where the sign bites
Counting back four days from Monday (day 1) is (1 − 4) mod 7. A truncating % gives −3, which is not a day number at all; floored and Euclidean both give 4, a Thursday. The same thing happens when an array index steps back from 0. The usual fix, ((a % m) + m) % m, gives the floored answer rather than the Euclidean one, so it still returns a negative number when m is negative. The calculator evaluates that expression for your inputs too.
The inverse and the power
The modular inverse is the modular version of division: the number x with a·x ≡ 1 (mod m). It exists exactly when gcd(a, m) = 1, and the extended Euclidean algorithm produces it alongside the Bézout coefficients. Modular exponentiation, ab mod m, is computed by repeated squaring: one squaring for each binary digit of b after the first and one multiplication for each 1 bit, instead of b − 1 multiplications. RSA and Diffie–Hellman both use modular exponentiation. Recovering the exponent b from a, m and the result is called the discrete logarithm problem; in RSA the exponent is public and the unknown is the base, which is a different problem.
Every figure on this page is exact. The calculator works in JavaScript’s arbitrary-size whole numbers (BigInt) rather than floating point, so a modulus of 300 digits is handled as exactly as 26, and it refuses input it cannot read rather than guessing. It all runs in your browser.
Where truncated, floored and Euclidean remainders part ways
With a positive divisor, floored and Euclidean remainders are always equal; only a negative m tells them apart.
The fix ((a % m) + m) % m gives the floored remainder, so it is negative when m is.
Java’s Math.floorMod(+4, -3) is −2: floored, with the sign of the divisor (Java SE 21 API).
Python’s Decimal remainder takes the sign of the dividend, unlike Python’s own % on integers (Python docs).
In C, dividing by zero with % is undefined behaviour; Python raises ZeroDivisionError and JavaScript returns NaN.
A modular inverse exists only when gcd(a, m) = 1.
This calculator runs in your browser; nothing is uploaded.
Frequently Asked Questions
- It is the remainder when a is divided by m. This calculator’s headline answer is the Euclidean remainder, from 0 to |m| − 1, so it is never negative: −1 mod 26 is 25. That is the usual convention for a positive modulus. The table under the results also shows the truncated and floored answers that programming languages give.
- They round the quotient differently. JavaScript’s % truncates −7 ÷ 3 toward zero, to −2, which leaves −1; C, Java, Go, Rust and PHP do the same. Python’s % floors it, to −3, which leaves 2; Excel’s MOD does the same. Both satisfy −7 = 3·q + r. This calculator’s Euclidean answer is also 2, and it differs from Python’s only when the modulus is negative.
- The Euclidean remainder, the GCD, the inverse and the power all use |m|, so 7 mod −3 is 1, the same as 7 mod 3. Floored division gives −2 for the same pair, with the sign of the divisor, as Python’s % and Java’s Math.floorMod do. The conventions table shows both.
- The modular inverse of a (mod m) is the number x for which a·x ≡ 1 (mod m), the modular equivalent of dividing. It exists only when a and m are coprime, that is, when their greatest common divisor is 1. The calculator finds it with the extended Euclidean algorithm.
- For any two integers a and m, there are integers x and y with a·x + m·y = gcd(a, m). Those x and y are the Bézout coefficients, produced by the extended Euclidean algorithm. When the gcd is 1, x reduced mod m is the modular inverse of a.
- It is computing a^b mod m: raising a to a power and taking the remainder. The calculator uses repeated squaring, reducing mod m after every step, so it needs at most two steps per binary digit of b rather than b of them, and the result reports the exact count. RSA and Diffie–Hellman both use this operation.
- Yes, when a modular inverse exists. A negative exponent is read as a power of the inverse: a^(−b) mod m means (a⁻¹)^b mod m. If a has no inverse modulo m, the calculator says the negative power is undefined.
- Up to 1,000 digits in each field, and every result is exact. The calculator uses whole-number BigInt arithmetic rather than floating point, so it never rounds a long number. Input it cannot read, such as 0x1A, 2.5, 1e3 or (7), is refused with a message instead of being guessed at.
- No. Every calculation runs in your browser with a small built-in engine. Nothing you type is uploaded, and the tool keeps working offline once the page has loaded.
- Yes, with no account, sign-up or usage limit.
Method & sources
How it computes
a mod m is the Euclidean remainder, 0 ≤ r < |m| (−1 mod 26 = 25; 7 mod −3 = 1), shown beside the truncated and floored remainders (Leijen 2001). gcd and Bézout coefficients by the extended Euclidean algorithm; the inverse when gcd = 1; a^b mod |m| by repeated squaring, a negative b read as a power of the inverse. Exact BigInt arithmetic on decimal integers of up to 1,000 digits; any other input is refused, never rounded.
What this tool implements
- The extended Euclidean algorithm returns x and y with a·x + m·y = gcd(a, m); when the gcd is 1, x is the inverse of a modulo m (NIST Dictionary of Algorithms and Data Structures).
- Three conventions, as Leijen defines them: truncated, q = trunc(a/m), the remainder has the sign of a; floored, q = ⌊a/m⌋, the remainder has the sign of m; Euclidean, 0 ≤ r < |m|. The calculator reproduces Leijen's 8-row comparison table exactly. A negative modulus is read as |m|, as the Euclidean rule gives.
- Which convention each language uses is taken from its own documentation: ECMAScript Number::remainder truncates; C11 (N1570 draft) discards the fractional part; the JLS rounds toward 0 and Math.floorMod has the sign of the divisor; Go and Rust truncate and Rust's rem_euclid is the least non-negative remainder; PHP takes the sign of the dividend; Python's % the sign of the second operand and Decimal the dividend's; Excel MOD the sign of the divisor. JavaScript, C, PHP, Python and Ruby were also run; Java, Go, Rust and Excel were not.
- Repeated squaring: for an exponent of k binary digits the loop does k − 1 squarings and one multiplication per 1 bit, each reduced mod |m|; the page prints the counts it actually performed.
- Corrected 2026-11-05: modular power lost precision above m ≈ 94,906,265; integers past 2^53 were rounded silently; "0x1A" read as 0, "17abc" as 17 and "(7)" as −7 while "−7" was refused; an unreadable exponent was ignored; the prose called the discrete logarithm the basis of RSA; the range was given as 0 to m − 1 for a negative m; an unsourced 1801 date for Gauss's congruence notation and unsourced ISBN, hash-table and CRC claims were removed.
Sources
- Black PE, ed. Dictionary of Algorithms and Data Structures: extended Euclid's algorithm. National Institute of Standards and Technology. https://xlinux.nist.gov/dads/HTML/extendEuclid.html
- Leijen D. Division and Modulus for Computer Scientists. 2001; posted by Microsoft Research. https://www.microsoft.com/en-us/research/publication/division-and…
- Ecma International TC39. ECMAScript Language Specification (2027 draft), 6.1.6.1.6 Number::remainder and 6.1.6.2.6 BigInt::remainder. https://tc39.es/ecma262/
- ISO/IEC JTC1/SC22/WG14. N1570 Committee Draft, ISO/IEC 9899:201x, 12 April 2011, 6.5.5 Multiplicative operators. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
- Oracle. The Java Language Specification, Java SE 21 Edition, 15.17 Multiplicative Operators. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html
- Oracle. Math (Java SE 21 & JDK 21): floorMod. https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java…
- The Go Programming Language Specification: Arithmetic operators. https://go.dev/ref/spec
- The Rust Reference: Operator expressions; Rust standard library, i32::rem_euclid. https://doc.rust-lang.org/std/primitive.i32.html
Show 4 more
- PHP Manual: Arithmetic Operators. https://www.php.net/manual/en/language.operators.arithmetic.php
- Python Software Foundation. The Python Language Reference, 6. Expressions (binary arithmetic operations). https://docs.python.org/3/reference/expressions.html
- Python Software Foundation. decimal — Decimal fixed-point and floating-point arithmetic. https://docs.python.org/3/library/decimal.html
- Microsoft Support. MOD function. https://support.microsoft.com/en-us/excel/functions/mod-function