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

Nick Coghlan ncoghlan at gmail.com
Wed Mar 17 22:33:19 CET 2010

Steven D'Aprano wrote:
> On Thu, 18 Mar 2010 07:44:21 am Raymond Hettinger wrote:
>> The spectrum of options from worst to best is
>> 1) compare but give the wrong answer
>> 2) compare but give the right answer
>> 3) refuse to compare.
> Why is 3 the best? If there is a right answer to give, surely giving the 
> right answer it is better than not?

I agree with Steven here - for mixed *arithmetic* refusing to get
involved is a reasonable choice, because the whole point of Decimals is
to get the answers according to base 10 expectations. Allowing implicit
conversion of base 2 floats puts that foundation at risk.

In what way do comparisons carry the same risk? Decimal.from_float shows
that a perfectly adequate mapping from float into the Decimal space can
be made, and the comparisons have a clear well-defined answer. It may be
slightly confusing to those not familiar with the subtleties of limited
precision base 2 vs larger precision base 10, but the boolean result
places them in a clearly different category to my mind.


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

More information about the Python-Dev mailing list