HELP: restore my faith in Python
Fredrik Lundh
effbot at telia.com
Fri Mar 3 14:54:28 EST 2000
More information about the Python-list mailing list
Fri Mar 3 14:54:28 EST 2000
- Previous message (by thread): HELP: restore my faith in Python
- Next message (by thread): HELP: restore my faith in Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Steven Holton wrote: > I'm trying to learn Python, and wrote this script last night. I was not > expecting to see this behavior, and my faith is severely shaken: > > nose-46> python > Python 1.5.2 (#1, Sep 17 1999, 20:15:36) [GCC egcs-2.91.66 > 19990314/Linux (egcs- on linux-i386 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam > >>> limit = 5 > >>> for target in range(10): > ... n=((target/(limit+0.0))-(target/limit))*limit > ... print target, n, int(n) > ... > 0 0.0 0 > 1 1.0 1 > 2 2.0 2 > 3 3.0 3 > 4 4.0 4 > 5 0.0 0 > 6 1.0 0 <=== huh? > 7 2.0 1 <=== et tu Brutus? > 8 3.0 3 > 9 4.0 4 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... </F>
- Previous message (by thread): HELP: restore my faith in Python
- Next message (by thread): HELP: restore my faith in Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list