by sltkr · 14y ago · 0 comments
self._len = int(ceil(float(stop - start) / step))
Python supports arbitrary-length integers; you can't just cast those to (fixed-length) floating point numbers without losing precision. It's better to use integer division here, for example: self._len = (stop - start)//step + bool((stop - start)%step)
(A variant like (stop - start + step - 1)//step works only for positive numbers; I guess it could work if you put it in the earlier if-clause and put a corresponding assignment with +1 at the end in the other branch.)However, I understand if that complaint is just too nitpicky for you to want to mention it. Great post btw.
For example, int(float(10¹⁸ - 1)) != 10¹⁸ - 1, but xrange(1, 10¹⁸) is perfectly valid (even in Python 2).
edit: how do I type two consecutive asterisks on Hacker News? Backslash doesn't seem to work as an escape character.
For example, int(float(10**18 - 1)) != 10**18 - 1,
but xrange(1, 10**18) is perfectly valid (even in Python 2).Edit: failure. That's two stymied people :(