While using the built-in range function a while ago. I found an odd (perhaps bug) where the range function couldn't use float steps. I am not sure if that was intended for simplicity or not, but I wrote my own range function that goes beyond that anyway.
1 2 3 4 5 6 7 8 9 10 | from decimal import Decimal
# replaces default range function to allow more functionality
def range(start, stop=None, step=1):
if stop==None: stop = start; start = 0
x = []
while start < stop:
x.append(Decimal(str(start)))
start += Decimal(str(step))
return x
|
I suspect it is this way because of floating point inaccuracy. Personally I would find a way to do what I want using whole integers, or the Decimal module. Relying on FP when you expect accuracy is a common pitfall.
Thanks for the comment! I updated the function so that it uses the decimal module.
It is definitely by design that range() and xrange() don't accept floats. You have to watch out for round-off error. Dealing with open and closed intervals is more complicated.
You might like to look at this recipe here:
http://code.activestate.com/recipes/577068/
which doesn't (yet) support decimal or fractions, but does handle floats.
Your recipe doesn't support counting downwards, like range(10, 0, -0.1). It is also much more expensive to run than the built-in range() function, hence significantly slower, due to repeated conversions to and from strings. And since most calling code will expect integers, not Decimals, it will break other code:
=> raises TypeError: list indices must be integers
Interesting. In reality though, for the purpose I used it in at least, I wasn't looking for a very advanced function at all...I was looking for a simple way to just get some floats. :)
Thanks for your comment. I really see what you mean now. :)