newbie seeks inaccurate arithmetic insight

Tim Peters tim.one at home.com
Sun Apr 29 21:51:53 EDT 2001


[Stanley Krute]
> 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
>
> ...
> Can someone provide one or more URLs that discuss the topic ??

Most decimal fractions aren't exactly representable in binary floating point.
Not unique to Python; same story in C, C++, Java, Perl, Fortran, etc.  See

    http://www.python.org/cgi-bin/moinmoin/RepresentationError

for details.

If you need exact decimal arithmetic, and can afford the expense of
simulating decimal arithmetic, your best bet for now in Python is to use the
FixedPoint class at

    ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/
        DataStructures/FixedPoint.py

Else you simply have to live with that binary floats are just approximations
(but good ones) to decimal fractions.

if-you-want-exact-buy-a-one-dollar-hand-calculator<wink>-ly y'rs  - tim





More information about the Python-list mailing list