Why Python's integer division floors (2010)
11–20 of 102 posts
Re: Why Python's integer division floors (2010)
#12Yes that makes perfect sense. So why is int(-1.5) == -1? It should be -2.
Re: Why Python's integer division floors (2010)
#13Yes that makes perfect sense. So why is int(-1.5) == -1? It should be -2.
edit: -3//2 == -2 for instance, since it's strictly integer division
Re: Why Python's integer division floors (2010)
#14Note the top comment by “ark” - there’s really no perfect solution here. In the floating-point case, you have to choose between negative remainders or potentially inexact results. And you definitely want integer division to work the same as float division.
That said, I don't really see why you would necessarily want float and integer division to behave the same. They're completely different types used for completely different things. Pick the one that is appropriate for your use case.
(It seems like abs(a % b) <= abs(b / 2) might be the right choice for floats which is pretty clearly not what you want for integers. I also just learned that integer division // can be applied to floats in Python, but the result is not an integer, for some reason?)
Re: Why Python's integer division floors (2010)
#15Yes that makes perfect sense. So why is int(-1.5) == -1? It should be -2.
Re: Why Python's integer division floors (2010)
#16Re: Why Python's integer division floors (2010)
#17Changing well known behavior for something no one is really going to need. The justification makes sense, but it breaks convention and the relationship with modulo doesn't need to hold for negative numbers.
I strongly disagree. I would estimate that in 90% of cases where I use modulo in languages that truncate (instead of flooring), I write (a % b + b) % b, or something similar, just to get the right behaviour. The exceptional cases are those where I can convince myself that negative numbers simply won't come up. (It's never because I actually want the other behaviour for negative numbers.) - When using modulo to access…
Re: Why Python's integer division floors (2010)
#18Changing well known behavior for something no one is really going to need. The justification makes sense, but it breaks convention and the relationship with modulo doesn't need to hold for negative numbers.
I strongly disagree. I would estimate that in 90% of cases where I use modulo in languages that truncate (instead of flooring), I write (a % b + b) % b, or something similar, just to get the right behaviour. The exceptional cases are those where I can convince myself that negative numbers simply won't come up. (It's never because I actually want the other behaviour for negative numbers.) - When using modulo to access…
Re: Why Python's integer division floors (2010)
#19Earlier quoted context omitted.
Python does follow the convention, but what I am wondering now is why did FORTRAN break it ?
Fortran is old – 1958 onwards. It has precedence here, though at what point it separated the two behaviours into mod and modulo functions I don’t know. Edit: From what I can tell, standardised in Fortran 90, presumably older than that.
http://python-history.blogspot.com/2010/08/why-pythons-integ...
Re: Why Python's integer division floors (2010)
#20Yes that makes perfect sense. So why is int(-1.5) == -1? It should be -2.
because it's a float and not an int, it's strictly talking about integers here, not floats. All int conversions of floats are converted by discarding the remainder afaik but I could be wrong here (e.g. int(1.9) == 1, and int(-1.9) == -1) edit: -3//2 == -2 for instance, since it's strictly integer division
Basically, there's no free lunch. Personally, I prefer truncating integer division in combination with a pair of remainder operators.