Boneyard Tools

Modulo Calculator (Remainder and Mod)

Enter a dividend and a divisor to get three results at once: the remainder from the % operator, the floored modulo, and the truncated integer quotient. Negatives are fully supported, so you can watch the two mod conventions agree or split apart depending on the signs. Each card explains which rule it follows.

How to compute a modulo

  1. Type the dividend, the number being divided, into the first field.
  2. Type the divisor, the number you divide by, into the second field.
  3. Read the three result cards: Modulo, Remainder and Quotient.
  4. Use Copy modulo to grab the floored modulo value on its own.

Examples

Both numbers positive

dividend 17, divisor 5
modulo 2, remainder 2, quotient 3

Negative dividend

dividend -7, divisor 3
modulo 2, remainder -1, quotient -2

Negative divisor

dividend 7, divisor -3
modulo -2, remainder 1, quotient -2

Frequently asked questions

What is the difference between the remainder and the modulo?

The remainder follows the dividend's sign, the way the % operator behaves in most languages, so -7 % 3 is -1. The floored modulo follows the divisor's sign, so the same inputs give 2. The two agree whenever the dividend and divisor share a sign and only split when their signs differ.

How does this calculator handle negative numbers?

The remainder is computed as dividend % divisor, which truncates toward zero and keeps the dividend's sign. The modulo is ((dividend % divisor) + divisor) % divisor, which shifts the result into the divisor's sign. So 7 and -3 give a remainder of 1 but a modulo of -2.

Is the modulo always positive?

Only when the divisor is positive. A positive divisor forces a non-negative modulo, while a negative divisor produces a non-positive modulo. That is why the sign of the divisor, not the dividend, determines the sign of this result.

How is the quotient defined?

The quotient uses truncated division, Math.trunc(dividend / divisor), which rounds toward zero rather than down. So -7 divided by 3 gives a quotient of -2, not -3. Multiplying the quotient by the divisor and adding the remainder always returns the original dividend.

Why can the divisor not be zero?

Division and the modulo operation are undefined when the divisor is zero, so the calculator rejects it with an error rather than returning a meaningless value or infinity.

Do the dividend and divisor have to be whole numbers?

No. The calculator accepts decimals, and the underlying % operator extends to them, so 5.5 divided by 2 gives a remainder and modulo of 1.5. Floating point rounding can appear with numbers that cannot be represented exactly in binary.

Which convention do programming languages use?

It varies. C, Java, Go and JavaScript use the truncated remainder shown here, so their % can be negative. Python and Ruby use the floored modulo instead, matching the Modulo card. Knowing which one your language uses avoids subtle bugs with negatives.

How do I force a non-negative result in code?

Apply the same trick the Modulo card uses: ((a % n) + n) % n. Adding n before the final modulo lifts a negative remainder back into the 0 to n minus 1 range whenever n is positive.

Is my calculation private?

Yes. Everything runs locally in your browser, so the numbers you enter are never uploaded to a server.

Learn more

Related tools