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

Terry Reedy tjreedy at udel.edu
Sun Sep 30 07:44:42 CEST 2012


On 9/29/2012 11:48 PM, Steven D'Aprano wrote:
> 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:

Questions about past releases are better directed to python-list (where 
Steven would have given same answer ;-). But anyway, please open a doc 
issue on the tracker to update that item.

> 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
>
>
>


-- 
Terry Jan Reedy



More information about the Python-Dev mailing list