CCalculate.Studio

% Modulo Calculator

The modulo operation finds the remainder left over when one number is divided by another. This calculator computes a mod b using the floored (mathematical) convention, where the result's sign always matches the divisor, and also shows the truncated (programming-language) result, where the sign matches the dividend — the two conventions agree for positive numbers but differ whenever a negative value is involved.

Última revisión: 2026-07-07

Understanding the sign conventions

The two conventions produce identical results whenever both numbers are positive, but diverge whenever a negative value is involved, as shown below.

abFloored result (sign of b)Truncated result (sign of a)
13533 (same — both positive)
−1352−3
13−5−23
−13−5−3−3 (same — both negative)
  • Floored modulo (used by default as the primary result here) is the convention most common in pure mathematics and number theory, and matches Python's built-in % operator.
  • Truncated modulo (shown as the secondary result) matches the built-in % operator in JavaScript, C, C++, Java, and most C-family languages, where the remainder's sign follows the dividend.
  • Modulo by zero is undefined and not computed by this calculator; the divisor must be a nonzero number.

What is the modulo operation?

The modulo operation, written a mod b (or a % b), returns the remainder when a is divided by b. For example, 13 mod 5 = 3, because 13 = 2 × 5 + 3 — dividing 13 by 5 gives a quotient of 2 with 3 left over. Modulo is fundamental to number theory (modular/clock arithmetic), computer science (hashing, cyclic indexing, checksums) and everyday reasoning about remainders and cycles (such as days of the week or clock hours).

There are two widely used mathematical conventions for defining the sign of the result when negative numbers are involved: floored modulo, in which the result always takes the same sign as the divisor b, and truncated modulo, in which the result always takes the same sign as the dividend a. Both are mathematically valid definitions of 'remainder' and agree exactly whenever a and b are both positive; they diverge only when a negative value is involved.

The choice of convention matters in practice because different programming languages implement different defaults: Python's % operator uses floored modulo, while JavaScript, C, C++, Java and most C-family languages use truncated modulo (matching the sign of the dividend). This calculator reports both results explicitly so either convention can be read off directly.

How to use this modulo calculator

  1. Enter the dividend (a) — the number being divided.
  2. Enter the divisor (b) — the number you are dividing by. The divisor cannot be 0, since division (and modulo) by zero is undefined.
  3. Read the floored-convention result (sign always matches the divisor) — the mathematical convention most common in number theory.
  4. Compare with the truncated-convention result (sign always matches the dividend) if you need to match a specific programming language's built-in % operator, and check the quotient and full division equation for a worked breakdown.

The floored vs. truncated modulo formulas

Floored: q = ⌊a ÷ b⌋, r = a − b × q (sign of r matches divisor b)
Truncated: q = trunc(a ÷ b), r = a − b × q (sign of r matches dividend a)
Example: 13 mod 5 = 3 (both conventions agree)
Example: −13 mod 5 → floored = 2, truncated = −3 (conventions differ)

Floored modulo defines the quotient as q = ⌊a ÷ b⌋ (rounded down, toward negative infinity), then r = a − b×q. This guarantees the remainder r always has the same sign as the divisor b (or is 0), and satisfies 0 ≤ r < |b| when b > 0. Worked example: 13 mod 5: q = ⌊13/5⌋ = 2, r = 13 − 5×2 = 3.

Worked example with a negative dividend: −13 mod 5 (floored): q = ⌊−13/5⌋ = ⌊−2.6⌋ = −3, r = −13 − 5×(−3) = −13 + 15 = 2. Note the result is positive (matching the positive divisor), which differs from the truncated result below.

Truncated modulo defines the quotient by rounding toward zero instead of toward negative infinity, then computes the remainder the same way; this guarantees the remainder always has the same sign as the dividend a (or is 0). Worked example: −13 mod 5 (truncated, the JavaScript/C/Java % result): quotient rounds −2.6 toward zero to −2, so r = −13 − 5×(−2) = −13 + 10 = −3 — negative, matching the negative dividend, unlike the floored result of +2 for the same inputs.

Common mistakes

  • Assuming all programming languages compute a mod b the same way for negative numbers — JavaScript, C, C++ and Java use truncated modulo, while Python and many mathematics texts use floored modulo, and the results differ whenever a is negative.
  • Forgetting that the floored result always shares the divisor's sign, not the dividend's — this is the opposite of what the truncated (JavaScript-style) convention gives.
  • Trying to compute a mod 0 — the modulo operation, like division, is undefined when the divisor is zero.
  • Using non-integer inputs and expecting integer-style modular arithmetic — this calculator accepts real numbers and applies the same floor/truncate logic, but many classic modular-arithmetic properties (such as clock arithmetic) are defined specifically for integers.

Preguntas frecuentes

What is the difference between floored and truncated modulo?

Floored modulo rounds the quotient toward negative infinity, so the remainder always has the same sign as the divisor. Truncated modulo rounds the quotient toward zero, so the remainder always has the same sign as the dividend. They agree when both numbers are positive but differ when either is negative — for example, −13 mod 5 is 2 under the floored convention but −3 under the truncated convention.

Why does −13 mod 5 give different answers in different calculators?

It depends on which sign convention is used. Floored modulo gives 2 (the result matches the positive divisor's sign and satisfies 0 ≤ r < 5). Truncated modulo gives −3 (the result matches the negative dividend's sign). Python's % operator returns 2 (floored); JavaScript's, C's and Java's % operators return −3 (truncated). Neither is 'more correct' — they are two different, equally valid mathematical definitions.

How do you calculate a mod b by hand?

Divide a by b to get the quotient, round it according to your chosen convention (down for floored, toward zero for truncated), then subtract b times that rounded quotient from a: remainder = a − b × quotient. For 13 mod 5: 13 ÷ 5 = 2.6, rounds to quotient 2 either way, so remainder = 13 − 5×2 = 3.

What does mod mean in modular arithmetic and clock arithmetic?

Modular arithmetic treats numbers as 'wrapping around' after reaching the modulus — the classic example is a 12-hour clock, where time is computed mod 12 (or mod 24 for 24-hour time). For example, 15 o'clock in 24-hour notation corresponds to 15 mod 12 = 3 on a 12-hour clock face. Modulo is the operation that produces this 'wrapped' remainder.

Can you take the modulo of a negative divisor?

Yes. Under the floored convention, a mod b (with negative b) produces a remainder with the same sign as b — for example, 13 mod −5 = −2, since 13 = (−3)×(−5) + (−2). Under the truncated convention, the remainder instead matches the dividend's sign: 13 mod −5 = 3 under truncation, since the quotient rounds toward zero to −2, giving 13 − (−5)×(−2) = 13 − 10 = 3.

Referencias

  1. Rosen KH. Elementary Number Theory and Its Applications. 6th ed. Pearson, 2010. (Division algorithm and modular arithmetic.)
  2. Leijen D. Division and Modulus for Computer Scientists. Utrecht University Technical Report UU-CS-2001-20, 2001. (Formal comparison of flooring, truncating and Euclidean division conventions.)
  3. ECMA International. ECMA-262: ECMAScript Language Specification, §13.15 (Remainder Operator). ecma-international.org.

Teoría de números · Todas las calculadoras

Calculadoras relacionadas