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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Mar 23 00:05:47 CET 2010

Mark Dickinson wrote:

> But the Fraction type is going to mess this up:  for Decimal +
> Fraction ->  Decimal, I don't see any other sensible option than to
> convert the Fraction using the current context, since lossless
> conversion isn't generally possible.

You could convert the Decimal to a Fraction, do the arithmetic
as Fractions, and convert the result back to Decimal using the
current precision.

I think this would satisfy the principle of performing the
calculation as though infinite precision were available, and
the final rounding is justified, since seen from the outside
we are performing a single operation on two numbers.

Another approach would be to convert the numerators and
denominators separately to Decimal, and then do

   a/b + c/d = (a*d + b*c) / (b*d)

using the usual rules of Decimal arithmetic. This would
incur more than one rounding, but the rules for Decimals
concern operations between two Decimals, and we have a
Decimal and a Fraction here, so all bets could be considered


More information about the Python-Dev mailing list