[Python-Dev] Decimal <-> float comparisons in py3k.
raymond.hettinger at gmail.com
Wed Mar 17 22:58:25 CET 2010
On Mar 17, 2010, at 1:59 PM, 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?
From the early days of the decimal module,
we've thought that mixed float-decimal operations
are 1) a bit perilous and 2) have few, if any good
Accordingly, any mixed operations should be explicit
rather than implicit:
Decimal('1.1') + Decimal.from_float(2.2)
is better than:
Decimal('1.1') + 2.2
To help the user avoid confusion, we flag the latter with a TypeError:
unsupported operand type(s) for +: 'Decimal' and 'float'.
Unfortunately, in Py2.x, implicit mixed comparisons do not
raise an exception, and instead will silently fail by giving
an incorrect answer:
>>> Decimal('1.1') < 2.2
IMO, raising the exception is the right thing to do.
Short of that though, if we're going to give a result,
it should at least be a correct one.
* right answers are better wrong
* but ill-formed questions are best not answered at all
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev