Incorrect Decimal-Float behaviour in comparison tests

Sergei Organov osv at javad.com
Fri Mar 3 18:27:26 CET 2006


"Fredrik Lundh" <fredrik at pythonware.com> writes:

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

Well, without reading the manuals I'd expect this to raise an exception
instead of yielding of meaningless and confusing result, consider:

>>> 1.0 + "0"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'float' and 'str'
>>> 1.0 > "0"
False
???

Though equal/unequal is entirely different story than less/greater,
IMHO. I mean

>>> 1.0 == "1.0"
False
>>> 1.0 != "1.0"
True

is perfectly fine.

-- Sergei.




More information about the Python-list mailing list