[Python-Dev] Mixing float and Decimal -- thread reboot
Mark Dickinson
dickinsm at gmail.com
Mon Mar 22 20:32:54 CET 2010
On Mon, Mar 22, 2010 at 7:00 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
>
> On Mar 22, 2010, at 11:26 AM, Mark Dickinson wrote:
>>
>> Just for the record, I'd also prefer Decimal + Fraction -> Decimal.
>
>
> Guido was persuasive on why float + Fraction --> float,
> so this makes sense for the same reasons.
>
> For the implementation, is there a way to avoid the double rounding
> in myfloat + myfrac.numerator / myfrac.denominator?
>
> Perhaps translate it to:
>
> f = Fractions.from_decimal(myfloat) + myfract # Lossless, exact addition
> return f.numerator / f.denominator # Only one decimal context rounding applied.
I'm not sure; I see a couple of problems with this. (1) It's fine
for the basic arithmetic operations that Fraction already supports,
but what about all the other Decimal methods that don't have Fraction
counterparts. (2) It bothers me that the Decimal -> Fraction
conversion can be inefficient in cases like Decimal('1e<large>');
currently, all Decimal operations are relatively efficient (no
exponential-time behaviour) provided only that the coefficients don't
get too large; large exponents aren't a problem.
I think getting this to work would involve a lot of extra code and
significant 'cleverness'. I'd prefer the simple-to-implement and
simple-to-explain option of rounding the Fraction before performing
the operation, even if this means that the whole operation involves
two rounding operations. It's not so different from what currently
happens for Fraction+float, or even int+float.
Mark
More information about the Python-Dev
mailing list