36

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 |
ivs
  • 503
  • 1
  • 4
  • 8
  • Sorry, I meant -1 not 1. I don't know the answer and I feel totally confused. – ivs Oct 22 '13 at 12:31
  • 9
    There is no answer. "Integer division" is not a well-defined concept and each language's specification can choose flooring or truncation towards 0 as its defined result for the operation. – Wooble Oct 22 '13 at 12:35
  • In Ruby the quotient is rounded toward -infinity. – Stefan Oct 22 '13 at 12:44
  • Thanks for the answer. I really tried searching explanation for it, but couldn't find. Probably there is good arguments behind this behaviour? – ivs Oct 22 '13 at 12:46
  • 20
    Come on, the question is clear: "What should integer division -1 / 5 return?" The rest of the post is just justification or incentive for this question. With just a little interpretation I can understand the question as "Why are different languages behaving differently here?" and this is a good question. No need to close down this post. – Alfe Oct 22 '13 at 12:50
  • 2
    @Wooble that's actually a good answer. – Stefan Oct 22 '13 at 12:53
  • 2
    There have been [many Questions about rounding and integer division](http://stackoverflow.com/search?q=integer+division+rounding) for various languages, often with an eye toward overcoming a "rounding down" behavior with "rounding up". Many of these approaches will help in understanding how rounding works for negatives. – hardmath Oct 22 '13 at 12:54
  • A nice paper with some C implementations of division and remainder that round different ways: [Division and Modulus for Computer Scientists](https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/) – Peter Cordes Nov 29 '16 at 11:03
  • This is not division by negative number. This is negative divided by positive. – Barmar Jun 16 '22 at 15:10

2 Answers2

50

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.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • However, when the divisor is negative the results are not what I would have expected. E.g. `5 // -2` gives quotient `-3` and remainder `-1`. I think quotient `-2` and remainder `1` would have made more sense (so that we always have `0 <= r < abs(b)` and, of course, `a = b*q+r`. – Marcus Junius Brutus Apr 25 '18 at 19:48
  • @MarcusJuniusBrutus Then you prefer Euclidean division. – Franklin Yu Oct 20 '18 at 03:29
7

Integer division is implementation specific. From Wikipedia's Modulo operation:

Many implementations use truncated division where the quotient is defined by truncation q = trunc(a/n), in other words it is the first integer in the direction of 0 from the exact rational quotient, and the remainder by r=an q. Informally speaking the quotient is "rounded towards zero", and the remainder therefore has the same sign as the dividend.

Knuth described floored division where the quotient is defined by the floor function q=floor(a/n) and the remainder r is

r = a - nq = a - n \left\lfloor {a \over n} \right\rfloor.

Here the quotient is always rounded downwards (even if it is already negative) and the remainder has the same sign as the divisor.

Stefan
  • 109,145
  • 14
  • 143
  • 218