[issue2531] float compared to decimal is silently incorrect.

Mark Dickinson report at bugs.python.org
Sun Mar 29 23:42:18 CEST 2009


Mark Dickinson <dickinsm at gmail.com> added the comment:

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.

----------
keywords:  -easy

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue2531>
_______________________________________


More information about the Python-bugs-list mailing list