Is there a way to step between 0 and 1 by 0.1?
I thought I could do it like the following, but it failed:
for i in range(0, 1, 0.1):
print i
Instead, it says that the step argument cannot be zero, which I did not expect.
Is there a way to step between 0 and 1 by 0.1?
I thought I could do it like the following, but it failed:
for i in range(0, 1, 0.1):
print i
Instead, it says that the step argument cannot be zero, which I did not expect.
To counter the float precision issues, you could use the
Decimal
module.This demands an extra effort of converting to
Decimal
fromint
orfloat
while writing the code, but you can instead passstr
and modify the function if that sort of convenience is indeed necessary.Sample outputs -
And if you do this often, you might want to save the generated list
r
The trick to avoid round-off problem is to use a separate number to move through the range, that starts and half the step ahead of start.
Alternatively,
numpy.arange
can be used.Add auto-correction for the possibility of an incorrect sign on step:
frange(start, stop, precision)
Increase the magnitude of
i
for the loop and then reduce it when you need it.EDIT: I honestly cannot remember why I thought that would work syntactically
That should have the desired output.