Last updated September 29, 2026 · Report an error
Modulo (mod) gives the remainder of a division, but many programming languages — including PHP's % operator — return a result that can be negative when the dividend is negative, which differs from the mathematical definition of modulo. This tool computes the correct, always-non-negative mathematical mod.
How it works
The mathematical mod is computed by taking the raw remainder and, if negative, adding the (absolute value of the) modulus back to bring it into the standard non-negative range.
- Enter a (dividend).
- Enter b (modulus).
- Click Calculate to see your results.
Examples
A negative dividend
−7 mod 3: PHP's % operator gives −1, but the mathematically correct mod is 2, since −1 + 3 = 2 and 2 is the standard non-negative representative.
Who should use it
- Debugging modular arithmetic bugs caused by negative-number edge cases in code.
- Computer science and number theory coursework.
Industry applications
- Computer science and software engineering
- Cryptography and number theory education
Advantages
- Shows both PHP's raw remainder and the corrected mathematical mod side by side.
- Clearly explains the distinction and why it matters.
Limitations
- Only computes a single mod operation — not a full modular arithmetic expression evaluator.
Common mistakes to avoid
- Directly using a language's raw % or remainder operator for modular arithmetic without checking whether it matches the mathematical mod definition for negative operands.
Best practices
- When implementing modular arithmetic in code, explicitly adjust the raw remainder (adding the modulus if negative) rather than assuming the built-in operator already does this.
Tips
- If code involving modulo produces unexpected negative results, that's almost always this exact language-level discrepancy — apply the correction shown here.