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

Nick Coghlan ncoghlan at gmail.com
Mon Mar 22 11:51:33 CET 2010


Raymond Hettinger wrote:
> On Mar 21, 2010, at 3:35 PM, Guido van Rossum wrote:
>>> It seems to me that Decimals and floats should be considered at
>>> the same level (i.e. both implement Real).
>> Agreed, but doesn't help. (Except against the idea that Decimal goes
>> on the "integer" side of Fraction, which is just wrong.)
> 
> Woohoo!  Glad you agree.
> I was concerned that idea
> was gathering a following.

Heck no, it was just a random late night thought from me, and even I
thought it was a somewhat dubious idea. I don't mind at all that it
since has been knocked soundly (and deservedly) on the head :)

> Reasoning for emitting a warning by default:
> 
> 1) Real actual use cases for mixed decimal / float operations are rare.
> 2) But accidental mixed decimal / float is an easy mistake to make.
> 3) Implicit coercion hides the error.
> 4) A warning flag gives you a chance to catch your error.
> 5) A warning is educational (it makes sure that you understand
> what your program is doing)
> 6). A warning is easily silenced either through a) the warnings module,
>      b) setting a context flag in decimal, or c) by making the coercion explicit
>      using Decimal.from_float().

I'll add another one to that list:

(7) For backwards compatible changes, it is easy to go from exception ->
warning -> no warning (if we later decide to take that second step).
Going from exception -> no warning -> warning (if we were to change our
minds the other way) is a lot less user-friendly.

(I was going to try to play devil's advocate and argue in favour of
float results and/or no warning, but I got nuthin' - Raymond's points
made too much sense to me).

A warning is nice in that you can mix decimals and floats at the
interpreter prompt with a single warning per session, but the warning
can still act as a pointer into the weird and wonderful world of binary
vs decimal floating point.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list