newbie seeks inaccurate arithmetic insight

Eric Renouf erenouf at opticominc.com
Sun Apr 29 23:22:22 EDT 2001


Stanley Krute wrote:
> 
> Hi
> 
> I just started on the Python learning path.
> 
> I tried some calculator stuff, and got this
> sort of result:
> 
>      http://static.userland.com/images/stanleydaily/pythonarithmetic.gif
> 
> I searched the Tutorial at http://www.python.org/doc/current/tut/tut.html
> and the Reference manual at http://www.python.org/doc/current/ref/ref.html
> for some discussion of why simple decimal arithmetic
> gives these sorts of inaccurate results. But I couldn't find anything.
> 
> Can someone provide one or more URLs that
> discuss the topic ??
> 
> Thanks
> 
> Stan
It's all in how the numbers are converted to a string.  Quoting from the
What's New In Python 2.0 page at
http://www.python.org/2.0/new-python.html.

"""
Taking the repr() of a float now uses a different formatting precision
than str(). repr() uses %.17g format string for C's sprintf(), while
str()
uses %.12g as before. The effect is that repr() may occasionally show
more decimal places than str(), for certain numbers. For example, the
number 8.1 can't be represented exactly in binary, so repr(8.1) is
'8.0999999999999996', while str(8.1) is '8.1'. 
"""

-- 
Eric Renouf
Software Engineer
Opticom Inc.
www.getiview.com



More information about the Python-list mailing list