Modulo Operator: Practical Uses in Arithmetics

Find the modulo operator: practical uses in arithmetics with clear results and the method shown.

Quick Facts

Floored modulo
a mod n = a - n x floor(a/n)
Remainder takes the sign of the divisor n. Used by Python and Ruby.
Truncated modulo
a mod n = a - n x trunc(a/n)
Remainder takes the sign of the dividend a. Used by C, Java, and JavaScript's % operator.
Euclidean modulo
0 ≤ r < |n|
Remainder is always non-negative, regardless of the signs of a or n.

Your Results

Calculated
Result (selected convention)
-
a mod n under the convention chosen above
Truncated remainder
-
a - n x trunc(a/n) (JavaScript's %)
Floored modulo
-
a - n x floor(a/n) (Python's %)
Euclidean modulo
-
Always in the range [0, |n|)

Ready

Enter a dividend and divisor, then press Calculate.

How the Modulo Operator Works

The modulo operation finds the remainder left over after dividing one number by another. Given a dividend a and a non-zero divisor n, "a mod n" is the value r such that a = n x q + r for some integer quotient q. The catch is that mathematicians, and different programming languages, do not always agree on how to choose q and r when a or n is negative — that disagreement is exactly what this calculator makes visible by computing all three common conventions side by side.

Formula and method

All three conventions start from the same division a/n but round the quotient differently, which changes the sign of the remainder:

  • Truncated (C, Java, JavaScript's %): q = trunc(a/n) (round toward zero), so r = a - n x trunc(a/n). The remainder has the same sign as the dividend a.
  • Floored (Python's %, Ruby): q = floor(a/n) (round toward negative infinity), so r = a - n x floor(a/n). The remainder has the same sign as the divisor n.
  • Euclidean: q is chosen so the remainder is always r ≥ 0, using r = a - |n| x floor(a/|n|). This is the convention used in number theory and always satisfies 0 ≤ r < |n|.

For positive a and positive n, all three formulas give the identical result. They only diverge when a or n is negative — for example, -7 mod 3 is -1 (truncated), 2 (floored), and 2 (Euclidean).

Common sources of error

  • Assuming one convention everywhere: JavaScript, C, and Java's % operator is truncated, while Python's and Ruby's % is floored — porting code between languages without adjusting for negative operands silently changes results.
  • Dividing by zero: a mod 0 is undefined; the calculator flags this instead of returning a meaningless value.
  • Forgetting non-integer inputs work too: the same floor/trunc formulas apply to decimal numbers, not just whole numbers — 7.5 mod 2 = 1.5 under the floored convention.

Checking your result

For the floored and Euclidean conventions with n > 0, the remainder must always satisfy 0 ≤ r < n — if your result falls outside that range, recheck the formula. A quick sanity check for any convention: (a - r) / n should equal an integer quotient with no remainder left over.

Applications

Modulo arithmetic shows up constantly in everyday computing and math:

  • Clock and calendar arithmetic: converting 25 hours to "1 hour past midnight" uses 25 mod 24; wrapping the 12-hour clock uses mod 12.
  • Cyclic array indexing: repeating patterns or wrapping around a fixed-size list uses index mod length so indices never run off the end.
  • Parity and divisibility checks: a number is even exactly when n mod 2 = 0, and more generally n mod k = 0 tests divisibility by k.
  • Checksums and hashing: credit-card Luhn validation, ISBN check digits, and hash-table bucket assignment all rely on modulo to fold large numbers into a fixed range.

Frequently Asked Questions

What is the difference between floored and truncated modulo?
Truncated modulo (used by C, Java, and JavaScript's % operator) rounds the quotient a/n toward zero, so the remainder takes the same sign as the dividend a. Floored modulo (used by Python and Ruby) rounds the quotient toward negative infinity, so the remainder takes the same sign as the divisor n. For positive a and n both methods agree; they only differ when a or n is negative.
Why does JavaScript's % give a different result than Python's % for negative numbers?
JavaScript's % implements truncated division, so -7 % 3 = -1 in JavaScript. Python's % implements floored division, so -7 % 3 = 2 in Python. Both are mathematically valid conventions for the remainder; they just choose a different sign rule when the dividend is negative.
What are practical uses of the modulo operator?
Common uses include clock and calendar arithmetic (wrapping hours with mod 12 or mod 24), cyclic array indexing (index mod array length), checking parity (n mod 2 is 0 for even numbers), divisibility tests, checksum digits such as the Luhn algorithm and ISBN check digits, and generating repeating or hashed patterns in programming.
Can the modulo operation work with decimal (non-integer) numbers?
Yes. The formula a mod n = a - n x floor(a/n) works for any real numbers, not just integers. For example, 7.5 mod 2 = 1.5 under the floored convention, since floor(7.5/2) = 3 and 7.5 - 2x3 = 1.5.