The Modulo Operation on Negative Numbers in Python

The modulo operation on negative numbers in Python

Unlike C or C++, Python's modulo operator (%) always return a number having the same sign as the denominator (divisor). Your expression yields 3 because

(-5) / 4 = -1.25 --> floor(-1.25) = -2

(-5) % 4 = (-2 × 4 + 3) % 4 = 3.

It is chosen over the C behavior because a nonnegative result is often more useful. An example is to compute week days. If today is Tuesday (day #2), what is the week day N days before? In Python we can compute with

return (2 - N) % 7

but in C, if N ≥ 3, we get a negative number which is an invalid number, and we need to manually fix it up by adding 7:

int result = (2 - N) % 7;
return result < 0 ? result + 7 : result;

(See http://en.wikipedia.org/wiki/Modulo_operator for how the sign of result is determined for different languages.)

Modulo for negative dividends in Python

In Python, modulo is calculated according to two rules:

  • (a // b) * b + (a % b) == a, and
  • a % b has the same sign as b.

Combine this with the fact that integer division rounds down (towards −∞), and the resulting behavior is explained.

If you do -8 // 5, you get -1.6 rounded down, which is -2. Multiply that by 5 and you get -10; 2 is the number that you'd have to add to that to get -8. Therefore, -8 % 5 is 2.

Modulo operation on a python negative decimal.Decimal and a positive int

Python behaves according to IBM's General Decimal Arithmetic Specification.

The remainder is defined as:

remainder takes two operands; it returns the remainder from integer division. […]

the result is the residue of the dividend after the operation of calculating integer division as described for divide-integer, rounded to precision digits if necessary. The sign of the result, if non-zero, is the same as that of the original dividend.

So because Decimal('-45') // D('360') is Decimal('-0'), the remainder can only be Decimal('-45').

Though why is the quotient 0 and not -1? The specification says:

divide-integer takes two operands; it divides two numbers and returns the integer part of the result. […]

the result returned is defined to be that which would result from repeatedly subtracting the divisor from the dividend while the dividend is larger than or equal to the divisor. During this subtraction, the absolute values of both the dividend and the divisor are used: the sign of the final result is the same as that which would result if normal division were used. […]

Notes: […]


  1. The divide-integer and remainder operations are defined so that they may be calculated as a by-product of the standard division operation (described above). The division process is ended as soon as the integer result is available; the residue of the dividend is the remainder.

How many times can you subtract 360 from 45? 0 times. Is an integer result available? It is. Then the quotient is 0 with a minus sign because the divide operation says that

The sign of the result is the exclusive or of the signs of the operands.

As for why the Decimal Specification goes on this route, instead of doing it like in math where the remainder is always positive, I'm speculating that it could be for the simplicity of the subtraction algorithm. No need to check the sign of the operands in order to compute the absolute value of the quotient. Modern implementations probably use more complicated algorithms anyway, but simplicity could be have an important factor back in the days when the standard was taking form and hardware was simpler (way fewer transistors). Fun fact: Intel switched from radix-2 integer division to radix-16 only in 2007 with the release of Penryn.

Python Negative Number modulo positive number

I am not sure about the formula but you can add x to the negative number such that (x+ negative number)>=0 and x is a multiple of mod value . This is right because
x % k = (x+ y*k) % k



Related Topics



Leave a reply



Submit