AJ Designer

Modulo Calculator

Try a quick example:

17 mod 5 =

2

Solution Details

All three conventions
ConventionQuotientRemainder
Truncated (C / JavaScript)Selected32
Floored (Python / Ruby)32
Euclidean (number theory)32
Show your work
  1. a = 17, b = 5
  2. Truncated: 17 − trunc(17 / 5) · 5 = 17 − (3) · 5 = 2
  3. Floored: 17 − floor(17 / 5) · 5 = 17 − (3) · 5 = 2
  4. Euclidean: remainder in [0, |5|) = 2
  5. Divisible: no GCD(17, 5) = 1
  6. Selected (Truncated (C/JS)): 17 mod 5 = 2
Supplementary
Divisible:
no
GCD(|a|, |b|):
1

All three conventions agree because both operands are non-negative.

Share:

Truncated Modulo (C / JavaScript)

Quotient is rounded toward zero. The remainder takes the sign of the dividend a. This is what the % operator returns in C, C++, Java, Go, and JavaScript.

a mod b = a − trunc(a / b) · b

Floored Modulo (Python / Ruby)

Quotient is rounded toward negative infinity. The remainder takes the sign of the divisor b — so it's non-negative whenever b is positive. This is Python's % operator.

a mod b = a − floor(a / b) · b

Euclidean Modulo (Number Theory)

The remainder is always in the range zero up to but not including |b|, regardless of either sign. Preferred in pure mathematics because the result depends only on the equivalence class.

a mod b = r, with 0 ≤ r < |b|

How It Works

Modulo (often written 'a mod b' or 'a % b') returns the remainder after integer division. For positive inputs everyone agrees: 17 ÷ 5 = 3 remainder 2, so 17 mod 5 = 2. The disagreement starts with negative numbers. C and JavaScript truncate the quotient toward zero, so (−17) ÷ 5 = −3 and the remainder is −17 − (−3)·5 = −2. Python (and Ruby) floor the quotient toward minus infinity, so (−17) ÷ 5 = −4 and the remainder is −17 − (−4)·5 = 3. The Euclidean convention used by mathematicians forces the remainder into the range 0 ≤ r < |b|, giving 3 for the same inputs. This calculator shows all three side by side so you can pick the one your language or textbook uses.

Example Problem

Compute −17 mod 5 under each of the three conventions.

  1. Truncated quotient: −17 / 5 = −3.4, and trunc(−3.4) = −3 (round toward zero).
  2. Truncated remainder: −17 − (−3)·5 = −17 + 15 = −2. JavaScript and C give −2.
  3. Floored quotient: floor(−3.4) = −4 (round toward minus infinity).
  4. Floored remainder: −17 − (−4)·5 = −17 + 20 = 3. Python gives 3.
  5. Euclidean: the remainder must lie in [0, 5). Adjust −2 by adding 5 once to get 3.
  6. All three answers (−2, 3, 3) satisfy a = q·b + r with the same a and b — the conventions differ only in which q they pick.

When the dividend is non-negative, all three conventions agree. The conflict only appears with a negative dividend or a negative divisor.

Key Concepts

Every modulo convention satisfies the identity a = q · b + r with a unique pairing of quotient q and remainder r once you fix the rounding rule. Truncated rounding picks q toward zero, floored picks q toward −∞, and Euclidean picks q so that r is non-negative. For a positive divisor, floored and Euclidean coincide. For a negative divisor, only Euclidean still guarantees a non-negative remainder. The Euclidean algorithm for computing gcd(a, b) reuses modulo repeatedly: gcd(a, b) = gcd(b, a mod b), terminating when the remainder reaches zero. Hash tables, ring buffers, clock arithmetic, and cyclic indexing all depend on modulo to wrap values into a fixed range.

Applications

  • Hashing — `hash(key) % tableSize` maps an arbitrary key into a fixed-size bucket array. Languages that return negative remainders (C, Java, JavaScript) require an extra `((h % n) + n) % n` to be safe for negative hashes.
  • Clock and calendar arithmetic — '7 hours after 22:00' uses (22 + 7) mod 24 = 5. Day-of-week math from a date offset uses (start + offset) mod 7. Euclidean modulo is the natural fit because hours and weekday indices must be non-negative.
  • Ring buffers and circular queues — `(head + 1) mod capacity` advances the index, wrapping back to 0 when it hits the end of the buffer.
  • Random number generation — linear congruential generators use Xₙ₊₁ = (a·Xₙ + c) mod m. The choice of m (often a power of 2) makes the modulo a single bitwise AND.
  • Cryptography — modular exponentiation a^b mod n is the core of RSA and Diffie–Hellman. The modulus is typically a 2048-bit or larger prime, so BigInt or an arbitrary-precision library is required.
  • Checksums and parity — ISBN-10, Luhn (credit card), and many other check-digit schemes compute (weighted sum) mod 10 or mod 11 to detect single-digit transcription errors.

Common Mistakes

  • Assuming `%` always returns a non-negative remainder. It doesn't in C, C++, Java, Go, or JavaScript — `(-17) % 5` is −2 in those languages, but 3 in Python. If you need a non-negative result, use `((a % b) + b) % b`.
  • Confusing modulo with the IEEE remainder. C's `fmod()` is truncated (like %); Java's `Math.IEEEremainder()` rounds the quotient to the nearest even integer, which can give a result with the opposite sign of `%`.
  • Using `%` on floats and expecting an exact zero. `1.1 % 0.1` is not exactly zero because 0.1 has no finite binary representation. Round or compare with a tolerance.
  • Forgetting that `b = 0` makes `a mod b` undefined. JavaScript returns NaN, Python raises ZeroDivisionError, C is undefined behavior. Always validate the divisor.
  • Calling % 'modulo' when it actually computes the truncated remainder. They coincide for non-negative inputs but diverge for negatives. Most language references use 'remainder' for `%` and reserve 'modulo' for the floored or Euclidean form.

Frequently Asked Questions

What is modulo?

Modulo (written 'a mod b' or 'a % b') returns the remainder after dividing a by b. For example, 17 mod 5 = 2 because 17 = 3·5 + 2. It's how clocks wrap (14:00 + 5 hours = 19:00, but 22:00 + 5 hours = (22 + 5) mod 24 = 3:00), how hash tables distribute keys across buckets, and how cryptographic primitives constrain values to a fixed range.

What is the formula for a mod b?

All three common conventions share the identity a = q·b + r, where r is the remainder and q is the integer quotient. They differ in how q is rounded. Truncated modulo (C, JavaScript) uses q = trunc(a/b) so r has the sign of a. Floored modulo (Python) uses q = floor(a/b) so r has the sign of b. Euclidean modulo picks q so that 0 ≤ r < |b|.

Why does (−17) mod 5 give different answers in C and Python?

C and JavaScript truncate the quotient toward zero: −17 ÷ 5 = −3.4, trunc(−3.4) = −3, so the remainder is −17 − (−3)·5 = −2. Python floors toward minus infinity: floor(−3.4) = −4, so the remainder is −17 − (−4)·5 = 3. Both are valid — they just enforce different sign conventions on the remainder. Use whichever matches your language's % operator, or use the Euclidean form (3) when you need a non-negative answer in any context.

Are modulo and remainder the same thing?

For non-negative inputs, yes. For negative inputs, no. Most language references reserve 'remainder' for the truncated operator (% in C, C++, Java, JavaScript, Go) and use 'modulo' more loosely for any of the three conventions — most often the floored one (Python's %) or the Euclidean one (Knuth's mod). Mathematicians usually mean Euclidean when they say 'a mod n', since that's the canonical representative of the equivalence class.

When should I use Python's modulo vs C's modulo?

Use whichever your language gives you, but be aware of the sign rule when the dividend can be negative. Python's floored modulo is convenient for periodic wraparound (hours of the day, ring buffer indices) because it returns a non-negative result whenever the divisor is positive. C's truncated modulo matches the hardware DIV instruction on most CPUs and pairs naturally with integer division that also truncates toward zero. If you need a guaranteed non-negative remainder in C or JavaScript, compute `((a % b) + b) % b`.

What is Euclidean modulo?

Euclidean modulo, popularized by Donald Knuth, forces the remainder into the range 0 ≤ r < |b| regardless of the sign of either operand. So (−17) mod 5 = 3 and (−17) mod (−5) = 3 — the divisor's sign no longer matters. This convention is the natural one in number theory because the remainder is the canonical representative of the equivalence class of a modulo |b|. Some languages expose it explicitly (Haskell's `mod` is floored; `Data.Euclidean.mod` is Euclidean).

How are modulo and GCD related?

The Euclidean algorithm computes gcd(a, b) by repeated modulo: gcd(a, b) = gcd(b, a mod b), terminating when the remainder hits zero. For example, gcd(48, 18) → gcd(18, 12) → gcd(12, 6) → gcd(6, 0) = 6. The algorithm works under any of the three modulo conventions because it only depends on the remainder being a non-negative value strictly less than the divisor — which all three eventually produce after one or two steps.

Does modulo work with floating-point numbers?

Yes. For floats, a mod b is still defined as a − q·b where q is some integer chosen by the rounding rule (trunc, floor, or Euclidean). For example, 5.5 mod 2.0 = 1.5 in all three conventions. The catch is that floating-point inputs whose values aren't exactly representable in binary (like 0.1) can yield surprising remainders due to round-off. When you need bit-exact results, work in integers and scale by a power of 10.

Reference: Standard truncated, floored, and Euclidean modulo definitions following Donald Knuth, The Art of Computer Programming, Vol. 1, §1.2.4, and the C99, Python, and ECMAScript language specifications for their respective % operators.

Modulo Formulas at a Glance

Every modulo convention fixes a unique pair (q, r) satisfying a = q·b + r. They differ only in how the integer quotient q is rounded:

Truncated: a mod b = a − trunc(a / b) · b
Floored: a mod b = a − floor(a / b) · b
Euclidean: a mod b = r where 0 ≤ r < |b|
  • trunc(x) rounds x toward zero (drops the fractional part)
  • floor(x) rounds x toward minus infinity
  • The Euclidean form just shifts the truncated remainder by |b| once when it comes back negative

For non-negative inputs all three pick the same q, so they agree on r. The conflict only appears when at least one of a or b is negative.

Worked Examples

Positive inputs (all agree)

Compute 17 mod 5

  • 17 ÷ 5 = 3.4. All three rounding rules pick q = 3 here (3 is already the truncated, floored, and Euclidean answer).
  • Remainder = 17 − 3·5 = 17 − 15 = 2.
  • Truncated, floored, and Euclidean all give 2 — the conventions only disagree on negatives.

17 mod 5 = 2 (every convention)

Negative dividend

Compute -17 mod 5

  • -17 ÷ 5 = -3.4. Truncated rounds toward zero → q = -3. Floored rounds toward -∞ → q = -4.
  • Truncated remainder: -17 − (-3)·5 = -17 + 15 = -2 (matches JavaScript, C, Java).
  • Floored remainder: -17 − (-4)·5 = -17 + 20 = 3 (matches Python).
  • Euclidean: force the remainder into [0, 5). Adjust -2 by +5 to get 3.
  • If you need a non-negative result in JavaScript, compute ((-17 % 5) + 5) % 5 = 3.

-17 mod 5 = -2 (truncated) / 3 (floored) / 3 (Euclidean)

Negative divisor

Compute 17 mod -5

  • 17 ÷ (-5) = -3.4. Truncated → q = -3. Floored → q = -4.
  • Truncated remainder: 17 − (-3)·(-5) = 17 − 15 = 2 (sign of dividend).
  • Floored remainder: 17 − (-4)·(-5) = 17 − 20 = -3 (sign of divisor).
  • Euclidean: remainder in [0, |-5|) = [0, 5). Adjust 2 → 2 (already in range).
  • Notice that only the Euclidean form ignores the divisor's sign — useful when the divisor itself can be negative.

17 mod -5 = 2 (truncated) / -3 (floored) / 2 (Euclidean)

Related Calculators

Related Sites