Modulo Calculator
Remainder and modulo, which differ for negative numbers.
Work out Modulo. Remainder and modulo, which differ for negative numbers. Says where the rule comes from.
Modulo
2
Remainder operator gives -1
These are genuinely different operations and this is a case where it shows. The % operator in most programming languages takes the sign of the dividend, giving -1. The mathematical modulo is defined to land in the range 0 to 2, giving 2. Code that indexes an array or wraps a clock wants the second; a language that offers only the first needs ((a % n) + n) % n.
How the Modulo Calculator works
Remainder and modulo are not the same operation, and negative numbers are where it shows. The % operator in most programming languages takes the sign of the dividend, so −7 % 3 is −1. The mathematical modulo is defined to land in the range 0 to n−1, so −7 mod 3 is 2. Both are shown.
Also known as: mod calculator · remainder calculator · modulus calculator · negative modulo calculator
Two operations, one symbol
The remainder and the mathematical modulo agree for positive numbers, which is why most people never learn they are different.
They part company on negatives. The % operator in C, Java, JavaScript and most other languages returns a remainder carrying the sign of the dividend, so −7 % 3 is −1. The mathematical modulo is defined to land in the range 0 to n−1, so −7 mod 3 is 2.
The bug it causes
Both answers are defensible — −1 and 2 differ by exactly 3, so they represent the same position on a cycle. The problem is that only one of them works as an array index.
Code that wraps backwards through a list, computes a clock position, or cycles through options in reverse will reach for % and get a negative index. It works perfectly until the first backwards step, which is what makes it such a persistent bug.
The fix is ((a % n) + n) % n: take the remainder, add the divisor to lift any negative into range, then reduce again.
Frequently asked questions
What is the modulo operation?
The remainder after division, defined to be non-negative for a positive divisor. 7 mod 3 is 1, and −7 mod 3 is 2.
Why do I get a negative result in my code?
Because % in C, Java, JavaScript and most other languages returns a remainder that takes the dividend's sign, not a mathematical modulo. To fix it, use ((a % n) + n) % n.
When does the distinction matter?
Whenever a negative value can reach the operation — wrapping an array index, computing a clock position, or cycling through a list backwards. For positive inputs the two agree, which is why the bug stays hidden until it does not.
What is modulo used for?
Checking divisibility, wrapping values into a fixed range, hashing, and every kind of cyclic arithmetic — clocks, calendars and cryptography all rest on it.
Put this calculator on your own site
Free to use, on any site, commercial or not. Paste this where you want it to appear. It is a plain iframe, so it works in WordPress, Squarespace, Wix, Webflow, Ghost and anything else that accepts HTML.
<iframe src="https://www.thecalclibrary.com/embed/modulo-calculator" width="100%" height="640" style="border:1px solid #e2e8f0;border-radius:12px" loading="lazy" title="Modulo Calculator"></iframe>The only condition is that the credit line stays visible. It sits inside the frame, so you do not have to do anything to keep it.
Related calculators
Prime Factorization Calculator
Prime factors, the divisor count, and where it comes from.
OpenGCF Calculator
Greatest common factor, with every common factor listed.
OpenBinary Calculator
Binary to decimal, hex and octal, with validation that works.
Open