Modulo Calculator (Remainder)

Find a mod n — both the truncated and floored remainder conventions.

Free modulo calculator. Compute the remainder a mod n in both the truncated convention (JavaScript, C, Java) and the floored convention (Python), plus the integer quotient. Privacy-first and runs entirely in your browser — nothing is sent anywhere. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

Why are there two different modulo results?

Languages disagree on the sign of the remainder. The truncated convention (JavaScript, C, Java) gives a remainder with the sign of the dividend a. The floored convention (Python's % and the mathematical "mod n") gives a remainder with the sign of the divisor n, always in the range 0 to |n|. They only differ when exactly one of the numbers is negative.

This calculator computes a mod n and shows the remainder under both common conventions plus the integer quotient. It is for programmers and students who need to know exactly what a remainder will be, especially with negative numbers where languages disagree.

How it works

Two definitions are computed from the dividend (a) and divisor (n):

  • Truncated (JavaScript %, C, Java): remainder = a − n × trunc(a ÷ n). Its sign follows the dividend a.
  • Floored (Python %, mathematical “mod n”): remainder = ((a % n) + n) % n, always in the range 0 to |n|. Its sign follows the divisor n.

The quotient shown is the truncated integer quotient, trunc(a ÷ n). The two remainders are identical whenever both numbers share a sign; they differ only when exactly one is negative. Division by zero is reported as undefined.

Example

With a = -17 and n = 5:

  • Truncated: -17 − 5 × (-3) = -17 + 15 = -2
  • Floored: ((-17 % 5) + 5) % 5 = (-2 + 5) % 5 = 3
  • Quotient (truncated): -3
a mod nTruncatedFloored
17 mod 522
-17 mod 5-23
17 mod -52-3

All arithmetic happens locally in your browser, so the numbers you enter never leave your device.

Where the difference actually matters in code

For most everyday uses of the % operator — checking if a number is even, finding array indices, cycling through values — both conventions give the same result because both operands are positive. The difference only shows up when one or both numbers is negative, and in those cases it can cause subtle bugs that are hard to trace.

Circular / wrapping arithmetic

A common use case is wrapping an index around a fixed-size range — for example, cycling through days of the week or implementing a ring buffer. If the index can go negative (from subtraction or a user-provided offset), the two conventions diverge:

// JavaScript — truncated % can return negative
function prevDay(today) {
  return (today - 1) % 7; // if today=0, returns -1 (wrong!)
}

// Correct in JS using floored-style workaround
function prevDay(today) {
  return ((today - 1) % 7 + 7) % 7; // always 0–6
}

Python’s % handles this naturally because it uses the floored convention and always returns a non-negative result when the divisor is positive.

Hash table indexing

When mapping a hash code (which may be negative in Java and C) to a bucket index, using % directly can produce a negative bucket number and an ArrayIndexOutOfBoundsException. The standard fix is ((hash % n) + n) % n or, in Java, Math.floorMod(hash, n).

Cryptographic and number-theoretic code

Mathematics defines mod n as the floored convention, producing a result in [0, n). Cryptographic algorithms expressed in mathematical notation therefore expect floored behavior. When translating pseudocode from a paper into JavaScript or C, the truncated % will silently produce wrong results for negative inputs. Always use a floored-mod helper function in these contexts.

Language reference

LanguageOperatorConventionNegative-dividend result
Python%FlooredAlways non-negative (if n > 0)
JavaScript%TruncatedSame sign as dividend
Java%TruncatedSame sign as dividend
C / C++% (C99+)TruncatedSame sign as dividend
Ruby%FlooredAlways non-negative (if n > 0)
Go%TruncatedSame sign as dividend