Incorrect Decimal-Float behaviour in comparison tests

Fredrik Lundh fredrik at pythonware.com
Fri Mar 3 11:53:41 EST 2006


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

you can find more information on the design of the decimal type
here:

    http://www.python.org/peps/pep-0327.html

and various issues with floating point are described here:

    http://docs.python.org/tut/node16.html

</F>






More information about the Python-list mailing list