Incorrect Decimal-Float behaviour in comparison tests

Tim Hochberg tim.hochberg at ieee.org
Fri Mar 3 12:35:29 EST 2006


Fredrik Lundh wrote:
> "Cassiano, Marco" wrote:
> 
> 
>>I have difficulties with a weird Python 2.4.2 behaviour in comparing
>>Decimal to Floats.
>>
>>For Example :
>>
>>
>>
>>>>>from decimal import Decimal
>>>>>a=Decimal('3.7')
>>>>>b=6.3
>>
>>>>>if a > b :print a,b,'a is greater than b - NOT TRUE!!!!'
>>
>>... else: print a,b,'b is greater than a - CORRECT'
>>...
>>
>>
>>3.7 6.3 a is greater than b - NOT TRUE!!!!
> 
> 
> is this
> 
>     >>> 1.0 > "0"
>     False
> 
> also a bug ?
> 
>     http://www.python.org/doc/ref/comparisons.html
> 
>     "Most other types compare unequal unless they are the
>     same object; the choice whether one object is considered
>     smaller or larger than another one is made arbitrarily but
>     consistently within one execution of a program."

It's not a bug, but it is a misfeature. In 3.0, I'd love to see 
nonsensical comparisons raise TypeError, but still keep the ability to 
sort lists of heterogeneous objects. One approach would be keep cmp(x,y) 
permissive in its arguments while making __gt__ and friends strict.

Sorting would need to fall back __cmp__, if __lt__ either raised a type 
error or returned a non int (e.g., numpy arrays). This could probably be 
done by munging PyObject_RichCompareBool, but I haven't looked into it 
in any detail.

My $0.02


-tim

[SNIP]




More information about the Python-list mailing list