[Python-Dev] Decimal & amp; lt; -& amp; gt; float comparisons in py3k.

Steven D'Aprano steve at pearwood.info
Sun Mar 21 23:32:20 CET 2010

On Mon, 22 Mar 2010 08:47:53 am Greg Ewing wrote:
> Nick Coghlan wrote:
> > That's fine - binary floats *are* surprising. That's why Decimal
> > exists in the first place.
> This argument could equally well be used the other way --
> someone using Decimal is doing so precisely because they
> *don't* want to be surprised, in which case they would
> probably prefer to get an exception.

Then they're in for a terrible, terrible disappointment. Rounding issues 
don't go away because you're using Decimal instead of float, and I 
can't imagine anyone would like an exception in the following cases:

>>> Decimal(1)/Decimal(3)*Decimal(3) == Decimal(1)
>>> Decimal(2).sqrt()**Decimal(2) == Decimal(2)
>>> Decimal(10**28)+Decimal(1)-Decimal(10**28) == Decimal(1)

Rounding isn't the only surprise:

>>> x = Decimal("NAN"); x == x

Decimals are floats, but using radix 10 instead of radix 2.

Steven D'Aprano

More information about the Python-Dev mailing list