## The modulo operation on negative numbers in Python

modulus of negative numbers in math

python modulo

absolute value of a negative number python

c modulo negative

negative number modulo c

what does the java modulo operator return when given a negative number

python integer division negative

I've found some strange behaviour in Python regarding negative numbers:

>>> -5 % 4 3

Could anyone explain what's going on?

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.)

**The modulo operation on negative numbers in Python,** The modulo operator, denoted by the % sign, is commonly known as a The % operator in Python yields integers with the same sign as the divisor. It means the basket should overflow into the negative side of the number line, in this case In Python and generally speaking, the modulo (or modulus) is referred to the remainder from the division of the first argument to the second. The symbol used to get the modulo is percentage mark i.e. ‘%’. In Python, the modulo ‘%’ operator works as follows: The numbers are first converted in the common type.

Here's an explanation from Guido van Rossum:

http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html

Essentially, it's so that a/b = q with remainder r preserves the relationships b*q + r = a and 0 <= r < b.

**How Python's Modulo Operator Really Works,** The modulo operator returns the remainder of a division. But things get a little more tricky when you throw negative numbers into the mix. Java, 1, -1, 1, -1. Javascript, 1, -1, 1, -1. Ruby, 1, 2, -2, -1. Python, 1, 2, -2, -1 Python respects this definition, whereas in most other programming language the modulus operator is really more like a 'reaminder after division' operator. To compute the least residue of -5 % 11 , simply add 11 to -5 until you obtain a positive integer in the range [0,10] , and the result is 6.

There is no one best way to handle integer division and mods with negative numbers. It would be nice if `a/b`

was the same magnitude and opposite sign of `(-a)/b`

. It would be nice if `a % b`

was indeed a modulo b. Since we really want `a == (a/b)*b + a%b`

, the first two are incompatible.

Which one to keep is a difficult question, and there are arguments for both sides. C and C++ round integer division towards zero (so `a/b == -((-a)/b)`

), and apparently Python doesn't.

**Modulo of Negative Numbers - Torsten on Tech,** The role of floor divide. The Python integer division operator, //, is called floor divide. It's definition is: Floor divide x//y computes the greatest Python has a "true" modulo operation, while C has a remainder operation. It has a direct relation with how the negative integer division is handled, i.e. rounded towards 0 or minus infinite. Python rounds towards minus infinite and C(99) towards 0, but in both languages (n/m)*m + n%m == n, so the % operator must compensate in the correct direction.

**MODULUS operator in python with negative numbers,** It's further left on the number line. This means that floor division will always round away from zero for negative numbers, but towards zero for As pointed out, Python modulo makes a well-reasoned exception to the conventions of other languages. This gives negative numbers a seamless behavior, especially when used in combination with the // integer-divide operator, as % modulo often is (as in math. divmod):

**Tutorial Modulo Positive Modulo Negative (revised),** The emphasis is, sign of left operand is appended to result in case of modulus operator in C. It really means there exists an integer n that makes c as small as possible, but non-negative. a - n*b = c By hand, you can just subtract 2 (or add 2 if your number is negative) over and over until the end result is the smallest positive number possible: 3.14 % 2 = 3.14 - 1 * 2 = 1.14 Also, 3.14 % 2 * pi is interpreted as (3.14 % 2) * pi.

**Negative modulus in Python – axlesoft,** Think of it like moving a hand around a clock, where every time we get a multiple of N, we're back at 0. So, take mod 3 (in C and Python, it's n % 3)… Starting at The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem. The modulo operator is considered an arithmetic operation, along with +, -, /, *, **, //.

**Python's modulo operator and floor division,** Codey is an online platform to provide free education to everyone. Creating a platform to give your opinions, chat with others of similar interests. Learn and grow with open group chats for

**Modulus on Negative Numbers,** The integer division can be done on Python 3 too, with // operator, thus to get the 7 as a result, you can execute: 3 + 2 + 1 - 5 + 4 % 2 - 1 // 4 + 6. Also, you can get the Python style division on Python 2, by just adding the line. from __future__ import division. as the first source code line in each source file.

##### Comments

- looks right to me
`..., -9, -5, -1, 3, 7, ...`

- possible duplicate of C,Python - different behaviour of the modulo (%) operation
- You can use
`math.fmod`

to get the same behavior as in C or Java. - Surprisingly, Python's modulo operator (%)
**does not***always*return a number having the same sign as the denominator (divisor). See stackoverflow.com/questions/48347515/… - Languages like C++ and Java also preserve the first relationship, but they ceil for negative
`a`

, positive`b`

, whereas Python floors. It's always true that`abs(r) < b`

, and they ceil iff`r <= 0`

. - "It would be nice if a/b was the same magnitude and opposite sign of (-a)/b." Why would that be nice? When is that a desired behaviour?
- Thanks your example made me understand it :)
- @NullUserException - yup, it was. fixed. Thanks.