7 ms·
From slide 32: > (In Python 3 // is integer division) I find it clearer to describe this behavior as "floor" division. You might think "integer division" wou
by wyldfire 10y ago
From slide 32:
> (In Python 3 // is integer division)
I find it clearer to describe this behavior as "floor" division. You might think "integer division" would have the same results as python 3's `int(a / b)` when `a` and `b` are ints, but, no, this is not the case. I never could quite understand why all the hub-bub about "integer division" and why it should change from python 2 to python 3 because the behavior seemed natural and similar to the native integer division on most processors. Indeed its behavior is astonishingly different with negative numbers.
$ python2 -c 'print 2/3, -2/3, int(-2./3.)'
0 -1 0
$ python3 -c 'print(2//3, -2//3, int(-2.//3.))'
0 -1 -1
$ python3 -c 'print(2/3, -2/3, int(-2./3.))'
0.6666666666666666 -0.6666666666666666 0
- danbruc 10y agoIt is also worth noting that in most languages -2 / 3 yields 0 and not -1, i.e. integer devision rounds towards zero, not towards negative infinity.
- wyldfire 10y agoYes, in fact that's the point I'm trying to focus on but never made explicit. Thanks.
- tzs 10y agoInteger division rounding toward negative infinity makes it so that for integers a, b, with b > 0, then if a%b is defined as a - b * (a/b), you will always have 0 <= a%b < b. Integer division rounding toward zero makes that not so. a%b for negative a, positive b, will be <= 0. On the other hand, rounding toward negative infinity means that (-a)/b != -(a/b), when b does not divide a. Rounding toward zero makes (-a)/b = -(a/b). I think most mathematicians would rather have 0 <= a%b < b than (-a)/b = -(a/b) if they can't have both.
- danbruc 10y agoEric Lippert, a former C# compiler developer, has a good blog post [1] discussing the reasoning and the difference between modulus and remainder. [1] https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/whats-the-difference-remainder-vs-modulus/ https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/what...
- paulddraper 10y agoThe term "integer division" comes from the fact that Python 2 a/b is the same as Python 3 a//b if a and b are integers. It's not the same as int(a/b), because like most languages int() rounds toward zero, and unlike most languages Python division rounds down. 100% agreed that "floor division" is a better description (e.g. it covers floats too).
- eric_h 10y ago> like most languages int() rounds toward zero, and unlike most languages Python division rounds down As someone who only occasionally needs to manipulate python, and someone who rarely needs to do integer arithmetic on negative numbers (beyond addition/subtraction) this caused me to do a double-take. I'd always just assumed that languages that treat floats and integers separately would always round integer division towards zero, but TIL python rounds to the left on the number line. It's really true! Turns out it's true in ruby, too: -5 / 4 == -2 How 'bout that. Anyone know what other languages treat integer division in this way?
- jacobolus 10y agoIn both cases, b * (a // b) + a % b = a, which is often a behavior that you want. In some other languages, the % operator (IMO wrongly) takes the sign of a, making it more like a “remainder” operator than a “modulo” operator, whereas in Python % (IMO correctly) takes the sign of b. I also agree that this should be thought of as “floor division” rather than “integer division” though.
- nbadg 10y agoAlso, if you need simple (though not necessarily immediately obvious) ceil division, you can do this: -(-a // b) As in: >>> 1 // 2 0 >>> -(-1 // 2) 1 This works because the floor division is truly a floor (and not a truncate), so floor division of a negative number is "rounded" towards negative infinity, and not towards zero.
- slrz 10y agoWith stuff like this, any ports of existing Python 2 codebases are wont to lead to tons of subtle bugs. No wonder Python 3 adoption is as low as it is.
- ciupicri 10y agoThe // division is like the mathematical division where the remainder is positive (a = qd + r and 0 ≤ r < |d|). If -2 // 3 would give you 0, it means the remainder is -2 which is a no-no. If -2 // 3 gives you -1, the remainder is 1 which is fine.
- maxerickson 10y agoIt was introduced as floor division: https://www.python.org/dev/peps/pep-0238/ https://www.python.org/dev/peps/pep-0238/ And is currently documented as such: https://docs.python.org/3/tutorial/introduction.html#numbers https://docs.python.org/3/tutorial/introduction.html#numbers https://docs.python.org/3/glossary.html#term-floor-division https://docs.python.org/3/glossary.html#term-floor-division https://docs.python.org/3/library/operator.html#operator.floordiv https://docs.python.org/3/library/operator.html#operator.flo...