[Python-Dev] Mixing float and Decimal -- thread reboot

Adam Olsen rhamph at gmail.com
Sat Mar 20 18:04:15 CET 2010

On Sat, Mar 20, 2010 at 09:11, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Mark Dickinson <dickinsm <at> gmail.com> writes:
>> On Fri, Mar 19, 2010 at 9:50 PM, Guido van Rossum <guido <at> python.org>
> wrote:
>> > There is one choice which I'm not sure about. Should a mixed
>> > float/Decimal operation return a float or a Decimal?
>> I'll just say that it's much easier to return a Decimal if you want to
>> be able to make guarantees about rounding behaviour, basically because
>> floats can be converted losslessly to Decimals.  I also like the fact
>> that the decimal module offers more control (rounding mode, precision,
>> flags, wider exponent range) than float.
> A problem, though, is that decimals are much slower than floats. If you have a
> decimal creeping in some part of a calculation it could degrade performance
> quite a bit.

For a little context, we have this numeric tower:

int -> Fraction -> float -> complex

And coincidentally (or not), we have an unspoken rule that you can go
right, but never left (int/int -> float, int/Fraction -> Fraction,
Fraction/float -> float, Fraction/complex -> complex, etc).  This
gives us a preference for fast, inexact results.

Decimal is more precise, and pays a performance cost for it.  It also
seems odd to stick it between float and complex (nobody's planning a
ComplexDecimal, right?)  That suggests it should go between Fraction
and float.  Decimal/float -> float.

Adam Olsen, aka Rhamphoryncus

More information about the Python-Dev mailing list