[Python-Dev] "Decimal(2) != float(2)"???

Steven D'Aprano steve at pearwood.info
Sun Sep 30 05:48:07 CEST 2012


On 30/09/12 10:43, Jan Kaliszewski wrote:
> Hello,
>
> In http://docs.python.org/release/3.2.3/reference/expressions.html#in we
>read: "[...] This can create the illusion of non-transitivity between
>supported cross-type comparisons and unsupported comparisons. For example,
>Decimal(2) == 2 and 2 == float(2) but Decimal(2) != float(2)."
[...]
> Is it a bug in the docs or in Python itself? (I checked that in 3.2, but it may be true for 3.3 as well)

Documentation bug. It used to be the case that Decimal and float did not compare equal:

steve at runes:~$ python3.1
Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
py> from decimal import Decimal
py> Decimal(2) == 2.0
False

but starting in 3.2 they do. But of course there are traps for the unwary,
due to binary floats, e.g. Decimal("0.1") != 0.1



-- 
Steven


More information about the Python-Dev mailing list