Removing easy keyword, since I don't think it applies here.
The problem here is hashing: either we break the rule that
if two objects compare equal then they hash equal, or we
fix hash so that e.g., hash(Decimal('2.5')) == hash(2.5).
For the latter, the least invasive way to do it would be
to fix only the Decimal __hash__ method. For that, we
really need a Decimal -> float conversion, so that we
can do something like (for a Decimal x):
if x == Decimal.from_float(x.to_float()):
return hash(x.to_float())
[rest of hash method here]
The builtin float() (which converts a Decimal to a string
and then uses the standard C library's string -> float
conversion) probably isn't good enough for this, since
there are no requirements that it should be (even close to)
correctly rounded.
The bottom line: getting a correctly-rounded
Decimal -> float method, without knowing what
the float format is, is going to be hard.
If we assume IEEE 754 then it's much easier. |