Floating point bug?
Jeff Schwab
jeff at schwabcenter.com
Wed Feb 13 20:49:08 EST 2008
robinsiebler at gmail.com wrote:
> I've never had any call to use floating point numbers and now that I
> want to, I can't!
>
> *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32
> bit (Intel)] on win32. ***
>>>> float (.3)
> 0.29999999999999999
>>>> foo = 0.3
>>>> foo
> 0.29999999999999999
A classic (if lengthy) read:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
If you just want to see a pretty representation:
>>> print 0.3
0.3
If you need a pretty string for use in code:
>>> def pretty_fp(fpnum, prec=8):
... return ('%.8f' % fpnum).rstrip('0')
...
>>> pretty_fp(0.3)
'0.3'
More information about the Python-list
mailing list