Re: Comparing float and decimal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Gerhard_H=E4ring?=

    Re: Comparing float and decimal

    D'Arcy J.M. Cain wrote:
    I'm not sure I follow this logic. Can someone explain why float and
    integer can be compared with each other and decimal can be compared to
    integer but decimal can't be compared to float?
    >
    >>>from decimal import Decimal
    >>>i = 10
    >>>f = 10.0
    >>>d = Decimal("10.00" )
    >>>i == f
    True
    >>>i == d
    True
    >>>f == d
    False
    I can give you the technical answer after reading the sources of the
    decimal module: you can only compare to Decimal what can be converted to
    Decimal. And that is int, long and another Decimal.

    Everything else will return False when comparing.
    This seems to break the rule that if A is equal to B and B is equal to
    C then A is equal to C.
    Yes, but only if comparison from type(A) to type(C) is supported at all.
    Instead of raising ValueError or NotImplementedE rror, the decimal
    module returns False here.

    -- Gerhard

Working...