Comparing float and decimal
gh at ghaering.de
Tue Sep 23 14:45:33 CEST 2008
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
>>>> i == d
>>>> f == d
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 NotImplementedError, the decimal
module returns False here.
More information about the Python-list