HELP: restore my faith in Python

Tim Peters tim_one at email.msn.com
Fri Mar 3 21:59:13 EST 2000


[/F]
> welcome to the wonderful world of floating point
> arithmetics:
>
> >>> limit = 5
> >>> for target in range(10):
> ...    n=((target/(limit+0.0))-(target/limit))*limit
> ...    print target, "%.30g" % n, int(n)
> ...
>
> 0 0 0
> 1 1 1
> 2 2 2
> 3 3 3
> 4 4 4
> 5 0 0
> 6 0.99999999999999978 0
> 7 1.9999999999999996 1
> 8 3.0000000000000004 3
> 9 4 4
>
> I'll leave it to our resident IEEE guru, the timbot,
> to explain exactly why this is happening...

At the level of the original poster's question, the best IEEE-754 conformant
answer is "floating point sucks".  That's what everyone needs to learn
first.  The rest is mere elaboration, but takes years to complete.

when-the-student-is-ready-the-teacher-will-drink-beer-ly y'rs  - tim






More information about the Python-list mailing list