[Python-Dev] Decimal <-> float comparisons in py3k.

Steve Holden steve at holdenweb.com
Wed Apr 14 06:17:23 CEST 2010


Guido van Rossum wrote:
> On Tue, Mar 16, 2010 at 2:32 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> But mixed arithmetic runs into the problem, what do you want the result
>> type to be? Given (say) decimal+float, returning either a Decimal or a
>> float will be the wrong thing to do some of the time, so better to
>> prohibit mixed arithmetic and let folks handle their own conversions.
>> So +1 on continuing to prohibit mixed arithmetic.
> 
> I'm not disagreeing, but I really wonder, what is the value of
> supporting mixed comparisons then? Just because you *can* assign a
> meaning to it doesn't mean you should.
> 
> OTOH I'm sure a lot of people would like to see mixed arithmetic
> supported, the PEP be damned, and they would probably be happy with
> any simple rule about the return type even if it's not always ideal. I
> note that there are cases where converting a long to a float also is
> the wrong thing to do, and yet mixed long/float operations always
> return floats.

I suspect this latter behavior is a throwback to the days when
conversion of an integer to a float was guaranteed not to cause
overflow. With the extension of the integer type to longs that theory
went out of the window, and with it the previously manageable "widening"
that took place.

> If you are amenable to this argument, I would propose
> to make the result of mixed operations return a Decimal, since in some
> "intuitive complexity" sense an int is a simpler type than a float and
> a float is a simpler type than a Decimal -- so results return the more
> complex type. But my intuition on this isn't super strong and I could
> live with always returning a float as well -- there are always casts
> to force the issue.
> 
Alas Python now supports so many number systems that there is no longer
a rational (in the non-numerical sense) ordering of the systems which
allows a linear progression from one to the next. It therefore behooves
us to consider the implications of such a decision *very* carefully.
intuition alone (even yours, which I would back against most people's)
may not suffice.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/



More information about the Python-Dev mailing list