Does python suck or I am just stupid?
Ian Terrell
kife00 at yahoo.com
Tue Feb 25 16:32:30 EST 2003
More on floating point: floats *can* store exact values if they are
combinations of 1/(2^n), n=0,1,2,...
Your program runs "as expected" if you substitute
u_inc = 1.0/1024 for
u_inc = 0.001
(just for fun)
Ian
andy at takecover.net (Andy Mroczkowski) wrote in message news:<41948401.0302221000.6257443f at posting.google.com>...
> I've been tearing my hair out over a bug(?) I've come across. I've
> only been programming in Python for a few months so I'm no expert.
> I've seen the same behavior in Python2.2 on several different
> machines. Instead of banging my head against the wall even more, I
> thought I would ask the community for help.
>
> A good example of it is in the following code snippet.
>
>
> ########################### BEGIN #########################
> #!/usr/bin/env python
>
> 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
> ######################### END ##########################
>
> I would expect to see the following output:
> 0.0 0.0
> 0.0 0.0
> 0.0 0.0
> 0.25 0.25
> 0.25 0.25
> 0.5 0.5
> 0.5 0.5
> 0.75 0.75
> 0.75 0.75
> 1.0 1.0
> 1.0 1.0
> 1.0 1.0
>
> However, I just see:
> 0.0 0.0
> 0.0 0.0
> 0.0 0.0
>
>
> What is wrong? Is this some weird feature? Or is Python broken? And
> sorry if this is something silly that I'm overlooking on my part.
>
> Andy M.
More information about the Python-list
mailing list