[Python-Dev] Decimal data type issues

Bob Ippolito bob at redivi.com
Tue Apr 13 16:01:22 EDT 2004


On Apr 13, 2004, at 3:31 PM, Michael Chermside wrote:

> Facundo Batista writes:
>> Hash behaviour
>> --------------
>    [...]
>> In the PEP I wrote that Decimal must be hashable. But what
>> hash should it
>> give?
>>
>> Should the following be true?::
>>
>>   hash(Decimal(25) == hash(25)
>>   hash(Decimal.from_float(25.35) == hash(25.35)
>>   hash(Decimal('-33.8')) == hash(-33.8)
>>
>> I don't think so. I think that hash(Decimal(...)) just should return a
>> different value in each case, but no the same value that
>> other data types.
>
> I disagree. If x == y, then that should imply that hash(x) == hash(y).
> So hash(Decimal(25)) == hash(25) had better be true. On the other
> hand, hash(Decimal('-33')) CANNOT equal hash(Decimal('-33')), since
> the latter must equal hash(-33). And I'm not particularly concerned
> about floats... if it's easy to make the hashes equal for those few
> numbers which can be expressed EXACTLY as both a (binary) float and
> a (decimal) Decimal, then that's be "kinda nice", but since there
> are so few values which can be expressed exactly as both a float
> and a Decimal, I don't see it as a big issue.

 >>> hash(float(1.0)) == hash(1) == hash(1L)
True

Since this is already true, hash(Decimal.from_float(1.0)), 
hash(Decimal(1)), hash(Decimal(1L)), hash(Decimal('1.0')), 
hash(Decimal('1')), hash(Decimal(u'1.0')), etc. better be equal as 
well, whatever that takes.

As far as arbitrary floats hashing equally, I don't really care, but 
the simple ones definitely should.  Hopefully at some point, "float 
constants" (as in coming from source code or user input) in Python will 
actually be Decimal representations, so you don't lose precision until 
you ask for it.

-bob




More information about the Python-Dev mailing list