Skip to content
Calcrivo

Modulo Calculator

Compute JS remainder and true Euclidean modulo — they differ for negative numbers, shown side by side.

Inputs

The number being divided.

The number you are dividing by (the modulus).

Euclidean modulo (a mod b)

2

Always non-negative for positive divisor. The mathematical standard.

Truncated remainder (a % b)

-1

JavaScript, C, C++ and Java '%' operator. Sign matches the dividend.

Floored modulo

2

Python '%' and Ruby '%'. Sign matches the divisor.

Step by step

  1. Truncated division (JavaScript/C % operator)

    -7 = 3 × -2 + (-1)

    = a % b = -1

    Sign matches the dividend. This is what most programming languages call '%'.

  2. Euclidean modulo (always ≥ 0 when divisor > 0)

    -7 = 3 × -3 + 2

    = a mod b = 2

    ((-7 % 3) + 3) % 3 = 2

  3. Floored modulo (Python behaviour — sign matches divisor)

    -7 − ⌊-7/3⌋ × 3

    = 2

  4. Verification (Euclidean)

    3 × -3 + 2 = -7

    = ✓ Checks out

How it works

Modulo gives the remainder after division. For non-negative dividends all three conventions agree. They diverge only when the dividend or divisor is negative, because the conventions differ in which direction they round the quotient: toward zero (truncated), toward minus infinity (floored), or to the nearest integer that keeps the remainder non-negative (Euclidean). In everyday clockwork arithmetic (hours of the day, angles, hash table indices) the Euclidean version is what you want.

Formulas

Euclidean modulo

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

a
Dividend
b
Modulus (must be positive for non-negative result)

Truncated remainder (JavaScript %)

a % b = a − b × trunc(a/b)

Frequently Asked Questions

Why do −7 % 3 and −7 mod 3 give different answers?

−7 % 3 = −1 in JavaScript because −7 = 3 × (−2) + (−1) and the quotient rounds toward zero. −7 mod 3 = 2 in the Euclidean sense because −7 = 3 × (−3) + 2 and the quotient rounds toward negative infinity. The identity 'dividend = divisor × quotient + remainder' holds for both — they just choose different quotients.

Which version should I use in programming?

Use the Euclidean version (the formula ((a % b) + b) % b) for anything that must be non-negative: day-of-week arithmetic, wrap-around array indices, and hue angles. The raw % operator is fine when both operands are guaranteed to be positive.

Is modulo the same as the remainder in long division?

For positive integers, yes. For negatives they can differ depending on which quotient convention your long division uses.

You might also need