Message268045
> 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 |
|
Date |
User |
Action |
Args |
2016-06-09 17:54:33 | mark.dickinson | set | recipients:
+ mark.dickinson, Radosław Szalski |
2016-06-09 17:54:33 | mark.dickinson | set | messageid: <1465494873.23.0.58399102275.issue27265@psf.upfronthosting.co.za> |
2016-06-09 17:54:33 | mark.dickinson | link | issue27265 messages |
2016-06-09 17:54:32 | mark.dickinson | create | |
|