Rounding error

Tim Peters tim.one at home.com
Wed Apr 4 19:11:05 EDT 2001


[Matthew]
> I'm calculating a bunch of co-ordinates as below. I'm using
> Round(<coord>,2) but as you can see rounding errors are happening...
> Digit:  1 =  [123.53, 335.29000000000002, 358.81999999999999,
> 358.81999999999999, 382.35000000000002, 405.88, 429.41000000000003,
> 452.94, 476.47000000000003, 500.0]
>
> How do I overcome this? I'm using ActivePython Build203.

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