Source From Here
Question
Is there a range() equivalent for floats in Python?
How-To
Writing one like this shouldn't be too complicated:
For example:
As the comments mention, this could produce unpredictable results like:
You can use decimal.Decimal as the jump argument. Make sure to initialize it with a string rather than a float:
Or even:
And then:
Question
Is there a range() equivalent for floats in Python?
How-To
Writing one like this shouldn't be too complicated:
- def frange(x, y, jump):
- while x < y:
- yield x
- x += jump
As the comments mention, this could produce unpredictable results like:
You can use decimal.Decimal as the jump argument. Make sure to initialize it with a string rather than a float:
Or even:
- import decimal
- def drange(x, y, jump):
- while x < y:
- yield float(x)
- x += decimal.Decimal(jump)
And then:
沒有留言:
張貼留言