Mark Dickinson added the comment:
> the behavior differs simply based on whether the Decimal was created from a
> string vs a float
That's not quite right: a Decimal object keeps no knowledge of how it was
created. The behaviour differs depending on whether the value of the Decimal
happens to be exactly representable as a Python float or not. That's necessary
to ensure the invariant `x == y` implies `hash(x) == hash(y)` continues to hold
across types (though Python 3 has a better way of doing this).
So for example `Decimal('0.375')` was created from a string, but will hash
equal to the exactly equal float `0.375`:
noether:float-proofs mdickinson$ python2
Python 2.7.11 (default, May 1 2016, 08:20:00)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> hash(Decimal('0.375')), hash(Decimal(0.375))
(1610579968, 1610579968)
>>> hash(0.375)
1610579968
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue27265>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com