logoalt Hacker News

kubbyesterday at 8:32 PM2 repliesview on HN

It's not always the same:

  >>> hash(float('nan'))
  271103401
  >>> hash(float('nan'))
  271103657

Replies

zahlmanyesterday at 9:04 PM

Yes. The CPython hash algorithm for floats (https://github.com/python/cpython/blob/main/Python/pyhash.c#...) special-cases the non-finite values: floating-point infinities hash to special values modeled on the digits of pi (seriously! See https://github.com/python/cpython/blob/main/Include/cpython/...), and NaNs fall through ultimately to https://github.com/python/cpython/blob/main/Include/internal... which is based on object identity (the pointer to the object is used rather than its data).

1-moreyesterday at 9:39 PM

maybe it's that multiple bit patterns can be NaN and these are two different ones? In IEEE-754, a number with all the exponent bits set to 1 is +/-infinity if the fraction bits are all zero, otherwise it's NaN. So these could be values where the fractions differ. Can you see what the actual bits it's setting are?