for with decimal values?

Sion Arrowsmith sion at viridian.paintbox
Tue May 5 11:09:48 EDT 2009


Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
>En Sun, 03 May 2009 17:41:49 -0300, Zentrader <zentraders at gmail.com>  
>escribió:
>> 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
>
>Isn't so easy. You have representation errors and rounding errors here,  
>and they accumulate. [ ... ]

And remarkably quickly at that:

>>> ctr = -10.5
>>> for i in range(-105, 106):
...     i *= 0.1
...     if i != ctr: print repr(i), repr(ctr)
...     ctr += 0.1
...
-10.0 -10.000000000000002
etc.

-- 
\S

   under construction




More information about the Python-list mailing list