[Python-Dev] Decimal floats as default (was: discussion about PEP239 and 240)

Skip Montanaro skip at pobox.com
Wed Jun 22 21:22:16 CEST 2005


    Fredrik> Is there actually much code around that relies on the
    Fredrik> particular precision of 32- or 64-bit binary floats for
    Fredrik> arithmetic, and ceases working when higher precision is
    Fredrik> available? 

Umm, yeah...  The path you take from one or more string literals
representing real numbers through a series of calculations and ending up in
a hardware double-precision floating point number is probably going to be
different at different precisions.

    >>> x = Decimal("1.0000000000001")
    >>> y = Decimal("1.000000000000024567")
    >>> x
    Decimal("1.0000000000001")
    >>> y
    Decimal("1.000000000000024567")
    >>> float(x) 
    1.0000000000000999
    >>> float(y)
    1.0000000000000246
    >>> x/y
    Decimal("1.000000000000075432999999998")
    >>> float(x)/float(y)
    1.0000000000000753
    >>> float(x/y)
    1.0000000000000755

Performance matters too:

    % timeit.py -s 'from decimal import Decimal ; x = Decimal("1.0000000000001") ; y = Decimal("1.000000000000024567")' 'x/y'
    1000 loops, best of 3: 1.39e+03 usec per loop
    % timeit.py -s 'from decimal import Decimal ; x = float(Decimal("1.0000000000001")) ; y = float(Decimal("1.000000000000024567"))' 'x/y'
    1000000 loops, best of 3: 0.583 usec per loop

I imagine a lot of people would be very unhappy if their fp calculations
suddenly began taking 2000x longer, even if their algorithms didn't break.
(For all I know, Raymond might have a C version of Decimal waiting for an
unsuspecting straight man to complain about Decimal's performance and give
him a chance to announce it.)

If nothing else, extension module code that executes

    f = PyFloat_AsDouble(o);

or

    if (PyFloat_Check(o)) {
       ...
    }

would either have to change or those functions would have to be rewritten to
accept Decimal objects and convert them to doubles (probably silently,
because otherwise there would be so many warnings).

For examples of packages that might make large use of these functions, take
a look at Numeric, SciPy, ScientificPython, MayaVi, and any other package
that does lots of floating point arithmetic.

Like Michael wrote, I think this idea is DOA.

Skip


More information about the Python-Dev mailing list