Gerhard Häring wrote:
The new fractions module acts differently, which is to say, as most
would want.
True
Traceback (most recent call last):
File "<pyshell#2 0>", line 1, in <module>
F(1.0)
File "C:\Program Files\Python30\ lib\fractions.p y", line 97, in __new__
numerator = operator.index( numerator)
TypeError: 'float' object cannot be interpreted as an integer
True
True
so Fraction obviously does comparisons differently.
Decimal is something of an anomaly in Python because it was written to
exactly follow an external standard, with no concessions to what would
be sensible for Python. It is possible that that standard mandates that
Decimals not compare to floats.
tjr
D'Arcy J.M. Cain wrote:
>
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.
>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?
>>
>True
>True
>False
>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 = 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.
would want.
>>from fractions import Fraction as F
>>F(1) == 1.0
>>F(1) == 1.0
>>F(1.0)
File "<pyshell#2 0>", line 1, in <module>
F(1.0)
File "C:\Program Files\Python30\ lib\fractions.p y", line 97, in __new__
numerator = operator.index( numerator)
TypeError: 'float' object cannot be interpreted as an integer
>>F(1,2) == .5
>>.5 == F(1,2)
so Fraction obviously does comparisons differently.
Decimal is something of an anomaly in Python because it was written to
exactly follow an external standard, with no concessions to what would
be sensible for Python. It is possible that that standard mandates that
Decimals not compare to floats.
tjr
Comment