Academic managers online nowFree quote in 3 minutes. Assignments from $19.Submit assignment →
Free, no account, nothing storedThe arithmetic runs in your browser. Nothing you type is sent to us.All 100 calculators →

Mathematics · pattern A, Numeric fields

Modulo, and the negative-number trap.

Enter a value and a modulus for the remainder, with the mathematically non-negative version shown alongside the one most programming languages return.

Inputs

The formula used

a mod m is the remainder after dividing a by m

For negative a, most languages return a negative remainder while mathematics takes the non-negative one. Both are shown below.

a mod m

-2

Non-negative remainder
3
Quotient, floor division
-4
Quotient, truncated
-3
Divisible?
no
Congruence class
a ≡ 3 (mod 5)
Next value congruent to a
-12

Clock arithmetic, and why it bites in code.

Modulo wraps numbers into a fixed range, which is what makes clocks, calendars, hash tables and cyclic buffers work. The trap is negatives: −17 mod 5 is 3 in mathematics and −2 in C, Java and JavaScript, and code that indexes an array with the result crashes on the difference.

Questions about modulo

What does congruent modulo m mean?
That two numbers leave the same remainder when divided by m. 17 and 2 are congruent modulo 5.
Where is modular arithmetic used?
Cryptography, hashing, checksums, calendars and random number generation — anywhere values must wrap.
How do I force a non-negative result?
Use ((a % m) + m) % m, which is the value shown above.

A calculator handles the arithmetic. It cannot teach you the method.

If the number is not the part you are stuck on, that is what the service is for — a specialist who explains the working, not just the answer.

Arithmetic runs in double-precision floating point, so results beyond about fifteen significant figures are not exact. Where a question wants an exact fraction or surd, keep the exact form rather than a decimal.