Does python suck or I am just stupid?

Stefan Quandt squan at web.de
Tue Feb 25 08:35:19 EST 2003


> u = 0.0
> u_inc = 0.001
> 
> v = (0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0)
> 
> while u <= 1.0:
>         for x in v:
>                 if u == x:
>                         print u, x
>         u = u + u_inc
To avoid accumulation of small rounding errors while
successive adding of small float values in loops
it's preferable to write something like
 u = i * u_inc
 i += 1
instead of
 u = u + u_inc

This will make your problem disappear so you can
stop head banging :)

- Stefan




More information about the Python-list mailing list