Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer division by negative number [duplicate]

Tags:

python

math

ruby

What should integer division -1 / 5 return? I am totally confused by this behaviour. I think mathematically it should be 0, but python and ruby are returning -1.

Why are different languages behaving differently here? Please someone explain. Thanks.

| Language  | Code           | Result | |-----------+----------------+--------| | ruby      | -1 / 5         |     -1 | | python    | -1 / 5         |     -1 | | c         | -1 / 5         |      0 | | clojure   | (int (/ -1 5)) |      0 | | emacslisp | (/ -1 5)       |      0 | | bash      | expr -1 / 5    |      0 | 
like image 852
ivs Avatar asked Oct 22 '13 12:10

ivs


People also ask

How does integer division work with negative numbers?

RULE 1: The quotient of a positive integer and a negative integer is negative. RULE 2: The quotient of two positive integers is positive. RULE 3: The quotient of two negative integers is positive. If the signs are different the answer is negative.

What happens when you divide by negatives?

Two negatives make a positive, so a negative number divided by a negative number equals a positive number. For example, -8 / -2 = 4.

Is division possible for negative numbers?

Just like you can multiply and divide positive numbers, you can do the same with negative numbers. In order to multiply or divide negative numbers you must remember: If the signs are the same, the answer is positive. If the signs are different, the answer is negative.


1 Answers

Short answer: Language designers get to choose if their language will round towards zero, negative infinity, or positive infinity when doing integer division. Different languages have made different choices.

Long answer: The language authors of Python and Ruby both decided that rounding towards negative infinity makes more sense than rounding towards zero (like C does). The creator of python wrote a blog post about his reasoning here. I've excerpted much of it below.

I was asked (again) today to explain why integer division in Python returns the floor of the result instead of truncating towards zero like C.

For positive numbers, there's no surprise:

>>> 5//2 2 

But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

>>> -5//2 -3 >>> 5//-2 -3 

This disturbs some people, but there is a good mathematical reason. The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship (all variables are integers):

a/b = q with remainder r 

such that

b*q + r = a and 0 <= r < b (assuming a and b are >= 0). 

If you want the relationship to extend for negative a (keeping b positive), you have two choices: if you truncate q towards zero, r will become negative, so that the invariant changes to 0 <= abs(r) < otherwise, you can floor q towards negative infinity, and the invariant remains 0 <= r < b. [update: fixed this para]

In mathematical number theory, mathematicians always prefer the latter choice (see e.g. Wikipedia). For Python, I made the same choice because there are some interesting applications of the modulo operation where the sign of a is uninteresting. Consider taking a POSIX timestamp (seconds since the start of 1970) and turning it into the time of day. Since there are 24*3600 = 86400 seconds in a day, this calculation is simply t % 86400. But if we were to express times before 1970 using negative numbers, the "truncate towards zero" rule would give a meaningless result! Using the floor rule it all works out fine.

like image 61
Bill Lynch Avatar answered Sep 30 '22 09:09

Bill Lynch