Free Modulo of Negative Numbers Calculator

Enter a dividend and divisor to compute the modulo result.

Understanding Modulo with Negative Numbers

The modulo operation a mod na \bmod n is typically taught as the remainder after division, but when negative numbers enter the picture, the definition becomes ambiguous. Different conventions—especially in programming languages—determine whether the remainder is positive or negative. This free online modulo calculator handles negative dividends and divisors, allowing you to switch between the two most common interpretations: truncated division modulo and floored division modulo.

Two Common Models for the Modulo Operation

In number theory, the remainder is always non‑negative (Euclidean division), but practical implementations often diverge from this ideal. The two principal definitions used in software are:

  1. Truncated division modulo – the remainder is r=a−n⋅trunc⁡(a/n)r = a - n \cdot \operatorname{trunc}(a / n).
    The result inherits the sign of the dividend aa.

  2. Floored division modulo – the remainder is r=a−n⋅floor⁡(a/n)r = a - n \cdot \operatorname{floor}(a / n).
    The result inherits the sign of the divisor nn.

For positive numbers both definitions coincide, but for negative numbers they can produce different results.

How the Sign Rules Behave

The following table summarizes the sign of the remainder for every combination of dividend and divisor under each definition:

Dividend aaDivisor nnTruncated (sign = sign of aa)Floored (sign = sign of nn)
PositivePositivePositivePositive
NegativePositiveNegativePositive
PositiveNegativePositiveNegative
NegativeNegativeNegativeNegative

Examples That Illustrate the Difference

  • a=−9, n=4a = -9,\ n = 4
    Truncated: −9=4×(−2)−1-9 = 4 \times (-2) - 1 → remainder −1-1.
    Floored: −9=4×(−3)+3-9 = 4 \times (-3) + 3 → remainder 33.

  • a=9, n=−4a = 9,\ n = -4
    Truncated: 9=(−4)×(−2)+19 = (-4) \times (-2) + 1 → remainder 11.
    Floored: 9=(−4)×(−3)−39 = (-4) \times (-3) - 3 → remainder −3-3.

  • a=−9, n=−4a = -9,\ n = -4
    Both definitions use the same quotient 22: −9=(−4)×2−1-9 = (-4) \times 2 - 1.
    Remainder is −1-1 in both cases.

Why Two Definitions Exist

Programming languages have historically adopted either truncated or floored semantics for their mod operator. For instance, C and Java use truncated division for %, while Python employs floored division. Many languages now provide separate functions (e.g., Math.floorMod in Java) so that developers can choose the behavior most suitable for their application. The differences matter whenever you work with cyclic data structures, wrapping indices, or mathematical formulas that expect a non‑negative remainder.

Using This Negative Modulo Calculator

This tool lets you compute a mod na \bmod n for any integers—positive or negative—and see the result according to both the truncated and floored conventions side by side. You can also view the step‑by‑step division process, making it an ideal companion for debugging code or learning how modulo with negative numbers works. Simply enter the dividend and divisor, and the calculator instantly returns the remainder under your chosen definition.

Whether you are studying modular arithmetic or implementing algorithms in a language with a specific modulo behavior, this negative modulo calculator simplifies the verification of your manual calculations.

FAQ

1. What is the difference between truncated division modulo and floored division modulo?

Truncated division modulo uses the trunc() function to round the quotient toward zero, so the remainder has the same sign as the dividend. Floored division modulo uses floor() to round toward negative infinity, making the remainder share the sign of the divisor. For negative numbers these two definitions can give different remainders, while for positive numbers they are identical.

2. When is the remainder negative in a modulo operation?

Under truncated division, the remainder is negative whenever the dividend is negative. Under floored division, the remainder is negative when the divisor is negative. When both dividend and divisor are negative, both definitions yield a negative remainder.

3. How do I compute -9 mod 4 using the two methods?

Using truncated division: -9 ÷ 4 = -2.25, trunc(-2.25) = -2, remainder = -9 - 4×(-2) = -1. Using floored division: floor(-2.25) = -3, remainder = -9 - 4×(-3) = 3. So truncated gives -1, floored gives +3.

4. What does this negative modulo calculator do?

It computes the modulo operation for any integers, including negative dividends and divisors. You can select between truncated and floored division to see how the remainder changes. The tool also shows the underlying division steps, helping you understand the difference between the two conventions.

5. Do all programming languages use the same modulo rule for negative numbers?

No. C, C++, and Java use truncated division for their % operator, while Python uses floored division. Many languages now offer both variants through separate functions (e.g., Math.floorMod in Java). It's important to check the documentation of your specific language to avoid unexpected results.

How to Use

  1. Enter the dividend (a) in the first input field. This is the number being divided.
  2. Enter the divisor (n) in the second input field. This is the number you are dividing by.
  3. The calculator automatically computes both the truncated and floored modulo results in real time as you type.