9

Why does -103/100 == -2 but 103/100 == 1 in Python? I can't seem to understand why.

arshajii
  • 127,459
  • 24
  • 238
  • 287
frazras
  • 5,928
  • 4
  • 30
  • 40
  • Integer division rounds towards negative infinity. – Asad Saeeduddin Mar 19 '13 at 22:07
  • Are you asking how it works, or why it was designed to work this way? – abarnert Mar 19 '13 at 22:09
  • @abarnert both I guess – frazras Mar 19 '13 at 22:14
  • @frazras: I think Pavel Anossov's answer now covers both halves (as long as you understand why the division/modulo identity is important for arithmetic, and why everyone but C programmers expects modulo to return positive numbers for a positive base/divisor). – abarnert Mar 19 '13 at 22:20
  • Division [has been defined this way](http://en.wikipedia.org/wiki/Euclidean_division) for quite some time. – chepner Mar 19 '13 at 22:23
  • @abarnert I am happy you mentioned that! I was actually working in C++(Arduino), but I wanted to quickly check how the mod operator works for this computation, so I reached for the fastest way, a python shell and got this answer... Is it different for C/C++? – frazras Mar 19 '13 at 22:32
  • @frazras: For C99 and C++11, it's different (aka "wrong", because this is one of those holy-war issues like preferring Ruby, little-endian, or Windows). For C90 and C++03, it's not even different, it's implementation-defined, so you can't just learn the rule and compensate for it, you just can't write proper code. The Wikipedia link I put in a comment on Pavel Anossov's answer gives more details. – abarnert Mar 19 '13 at 22:44
  • @chepner: Actually, Euclidean division (as usually interpreted) gives a different answer than Python for negative divisor. Knuth explains why the Python way is better. (Again, see the Wikipedia link from my comment on the answer.) Some disagree (like Guy Steele and Niklaus Wirth), but who are you going to listen to, Knuth, or "some people"? :) – abarnert Mar 19 '13 at 22:49

2 Answers2

22

Integer division always rounds down (towards negative infinity).

http://www.mathsisfun.com/numbers/images/round-up.gif

Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the floor1 function applied to the result.

http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations

 

This allows for the integer division and modulo (remainder, %) operators to connect nicely through the identity x == (x/y)*y + (x%y).

 

1  floor(x) is the largest integer not greater than x.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • It might be worth adding a note on `(a//b)*b + a%b == a`. – abarnert Mar 19 '13 at 22:08
  • (+1) The picture is awesome. As far as the wording is concerned, I find it less ambiguous to say "rounds towards negative infinity" in preference to "rounds down". – NPE Mar 19 '13 at 22:14
  • [Wikipedia](http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation) actually has a pretty nice explanation of why most languages (C90 being a notable exception) either have truncated division and dividend-sign modulo, or floored division and divisor-sign modulo. Either one is reasonable (as is a third option, with always-positive modulus), but the way Python chose is more common in both number theory and practical arithmetic. – abarnert Mar 19 '13 at 22:24
  • I must give credit to www.mathsisfun.com as the source of the awesome picture. I hope this qualifies as fair use. – Pavel Anossov Mar 19 '13 at 22:25
1

Integer division takes (I believe) the floor() of whatever float comes out, more or less.

So that's -2 for the first division and 1 for the second.