for with decimal values?

MRAB google at mrabarnett.plus.com
Sun May 3 22:52:26 CEST 2009


Zentrader wrote:
> There is no need for a function or a generator.  A for() loop is a
> unique case of a while loop
> ## for i in range(-10.5, 10.5, 0.1):
> ctr = -10.5
> while ctr < 10.5:
>    print ctr
>    ctr += 0.1
> 
Python stores floats in binary, and 0.1 can't be held exactly as a
fractional binary number. Therefore it might be better to produce the
values as integers divided by 10:

for i in range(-105, 105):
     i = i / 10.0
     # do stuff here using i



More information about the Python-list mailing list