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

Steven D'Aprano steve at pearwood.info
Sat Mar 20 03:40:47 CET 2010

On Sat, 20 Mar 2010 08:50:04 am Guido van Rossum wrote:
> I'd like to reboot this thread. I've been spinning this topic in my
> head for most of the morning, and I think we should seriously
> reconsider allowing mixed arithmetic involving Decimal, not just
> mixed comparisons. [Quick summary: embed Decimal in the numeric tower
> but add a context flag to disallow implicit mixing of float and
> Decimal.]

The last few days, I've argued against changing the prohibition against 
mixed arithmetic operations. But you've inspired me to go take a look 
at a module I've been neglecting, fractions, and I've learned that the 
Fraction type already fully supports arithmetic and comparisons with 
floats and ints. I'm extremely impressed -- I had no idea the numeric 
tower in 2.6 was this advanced. (I still do most of my work with 2.5.)

Decimal appears to be the odd one:

>>> f = Fraction(0)
>>> d = Decimal(0)
>>> 0 == 0.0 == 0j == f
>>> 0 == 0.0 == 0j == f == d

Not just odd in the sense of "different", but also odd in the sense 
of "weird":

>>> d == 0 == 0.0 == 0j == f

> I'd like to look at the issue by comparing the benefits and drawbacks
> of properly embedding Decimal into the numeric tower. As advantages,
> I see consistent behavior in situations like the above and more
> intuitive behavior for beginners. Also, this would be a possible road
> towards eventually supporting a language extension where floating
> point literals produce Decimal values instead of binary floats. (A
> possible syntax could be "from __options__ import decimal_float",
> which would work similar to "from __future__ import ..." except it's
> a permanent part of the language rather than a forward compatibility
> feature.)

That's far more ambitious than I was willing to even imagine, but now 
that you've suggested it, I like it.

Steven D'Aprano

More information about the Python-Dev mailing list