Modulo Operations with Negative Numbers

Created by Anna Szczepanek, PhD
Reviewed by Rijk de Wet
Last updated: Nov 12, 2022

Here we address the non-obvious question of how the modulo operator works for negative numbers. In particular, we discuss how this works in programming languages.

If you're new to the modulos, check out our dedicated modulo calculator.

How does modulo work with negative numbers?

Recall that the modulo operator a mod n returns the remainder r of the division of a by n. More formally, in number theory, the result of the modulus operator is an equivalence class, i.e., the whole set of numbers that give the same remainder r when divided by n. In this set, we choose one number as the representative. Most often, this role is played by the remainder of the Euclidean division, which happens to be the smallest non-negative number in the equivalence class.

However, other ways to define the modulo operation are sometimes useful in modular arithmetics, especially when negative integers are involved. (For two positive numbers there's little doubt that the most useful version is that with the positive remainder.) Almost always, we want to have βˆ’n < r < n, so the actual choice is between the smallest positive representative and the largest negative one. In programming languages, there are two main implementations of mod, which choose the sign of the result depending on the signs of the dividend a and the divisor n:

  • Truncated division returns r = a βˆ’ n* trunc(a/n). Thus, r has the same sign as the dividend a.
  • Floored division returns r = a βˆ’ n* floor(a/n). Here, r has the same sign as the divisor n.

In most popular programming languages (such as Java, C, C++, Python), there are separate functions that can calculate the modulus according to both these definitions. There are also languages where only one of these operations is available. In case of doubt, consult the documentation before writing any code.

Now, we'll discuss how these two approaches work depending on whether the dividend or the divisor (or both) is negative.

When only the dividend is negative

If only the dividend is negative, then:

  • Truncated modulo returns the negative remainder; and
  • Floored modulo returns the positive remainder.

For instance, let's compute βˆ’9 mod 4. Clearly, we have

βˆ’9 = 4 * (βˆ’2) βˆ’ 1


βˆ’9 = 4 * (βˆ’3) + 3.

The equality in the first equation corresponds to the truncated division and implies that βˆ’9 mod 4 = βˆ’1. The dividend is negative, and thus so is the remainder.

The latter equation's equality corresponds to the floored version and implies that βˆ’9 mod 4 = 3. The divisor is positive, and thus so is the remainder.

When only the divisor is negative

If only the dividend is negative, then:

  • Truncated division returns the positive remainder; and
  • Floored division returns the negative remainder.

Let's answer the question about 9 mod (βˆ’4).

We have 9 = (βˆ’4) * (βˆ’2) + 1, which corresponds to the truncated version and implies that 9 mod (βˆ’4) = 1. The dividend and result are both positive.

Alternatively, 9 = (βˆ’4) * (βˆ’3) βˆ’ 3, which is the floored version and gives 9 mod (βˆ’4) = βˆ’3. The divisor and remainder are negative. See the floor function calculator if you're not yet familiar with it.

When both the divisor and dividend are negative

If both the divisor and dividend are negative, then both truncated division and floored division return the negative remainder.

Let's discuss (βˆ’9) mod (βˆ’4).

We have floor(βˆ’9/(βˆ’4)) = floor(9/4) = 2 and trunc(βˆ’9/(βˆ’4)) = trunc(9/4) = 2.

Hence, the equality (βˆ’9) = (βˆ’4) * 2 βˆ’ 1 corresponds to both truncated and floored division, and implies that in both cases we have (βˆ’9) mod (βˆ’4) = (βˆ’9) βˆ’ (βˆ’4)*2 = (βˆ’9) + 8 = βˆ’1. The dividend, the divisor, and the remainder are all negative.

Anna Szczepanek, PhD
x mod y = r
x (dividend)
y (divisor)
r (remainder)
Check out 40 similar algebra calculators πŸ”‘
Absolute value equationAbsolute value inequalitiesAdding and subtracting polynomials… 37 more
People also viewed…

Korean age

If you're wondering what would your age be from a Korean perspective, use this Korean age calculator to find out.


The LCM calculatorcalculates the least common multiple of two to fifteen numbers.


Do you always remember to put on sunscreen before going outside? Are you sure that you use enough? The Sunbathing Calculator β˜€ will tell you when's the time to go back under an umbrella not to suffer from a sunburn!

Triangle inequality theorem

The triangle inequality theorem calculator is here to check whether the three line segments can make up a triangle or not.
Copyright by Omni Calculator sp. z o.o.
Privacy policy & cookies
main background