0

Python documentation describes hashing procedure of fractions (and ints, and floats) here.

I understand the approach as far as 'treat fractions as mod P' and 'inverses mod P are easyish to compute'. Afterwards encode sign by negating hash when appropriate.

There is a additional rule:

If x = m / n is a negative rational number define hash(x) as -hash(-x). If the resulting hash is -1, replace it with -2.

This makes no sense to me since it leads to

hash(-1) == hash(-2)

Surely getting distinct values on common inputs is rather important for a good hash function!

How come this is a better choice than hash(-1) == -1?

The implementation in cPython gives no comments to this odd choice either.

Radost
  • 309
  • 3
  • 11
  • Isn't this just to avoid using `-1` as a hash value? If so, `-1` and `-2` are going to share a hash, but this doesn't really matter, hash alone isn't an indication of identity. To avoid this, all negative integer hashes could be shuffled down by one spot, but what net benefit would this have for anything? – tadman Feb 03 '23 at 18:30
  • 1
    @tadman It seems this requirement was very explicit. I'm still curious of the rationale https://peps.python.org/pep-0456/#requirements-for-a-hash-function – Radost Feb 03 '23 at 18:31

0 Answers0