Skip to content
ENExplore Calculators

Modulo Calculator

Compute the remainder of a division, with true mathematical modulo for negatives.

About Modulo Calculator

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)

Worked example

With the default values below, Modulo Calculator returns Euclidean modulo (a mod b) of 2. Change any input above and every figure here is recalculated in your browser.

Inputs used

Dividend (a)
-7
Divisor (b)
3

Results

Euclidean modulo (a mod b)Always non-negative for positive divisor. The mathematical standard.
2
Truncated remainder (a % b)JavaScript, C, C++ and Java '%' operator. Sign matches the dividend.
-1
Floored moduloPython '%' and Ruby '%'. Sign matches the divisor.
2

Step by step

  1. 1Truncated division (JavaScript/C % operator)= a % b = -1Sign matches the dividend. This is what most programming languages call '%'.
  2. 2Euclidean modulo (always ≥ 0 when divisor > 0)= a mod b = 2((-7 % 3) + 3) % 3 = 2
  3. 3Floored modulo (Python behaviour — sign matches divisor)= 2
  4. 4Verification (Euclidean)= ✓ Checks out

Assumptions and limits

  • The result is derived only from the 2 inputs listed above. Anything not among them is not part of the calculation.
  • In particular, this calculator does not take inflation, tax, fees or charges or compounding frequency into account.
  • Figures are rounded for display to whole numbers, so totals built from several rounded parts can differ slightly from the unrounded arithmetic.

How these calculators are built and tested is described on the Methodology page. If a figure looks wrong, please report it.

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.

Modulo Calculator – Calculate Euclidean modulo (a mod b)