logoalt Hacker News

swiftcoderyesterday at 8:43 PM2 repliesview on HN

The oddity here is not the float itself, it's that Python provided a default hash implementation for floats


Replies

_coggyesterday at 9:57 PM

Python supports arithmetic on mixed numeric types, so it makes sense that floats and ints should have a hash function that behaves somewhat consistently. I don't write a lot of python, but having used other scripting languages it wouldn't surprise me if numeric types get mixed up by accident often enough. You probably want int(2) and float(2) to be considered the same key in a dictionary to avoid surprises.

See: https://docs.python.org/3/library/stdtypes.html#hashing-of-n...

JuniperMesosyesterday at 8:53 PM

Yeah IEEE 754 floating point numbers should probably not be hashable, and the weird (but standard-defined) behaviour with respect to NaN equality is one good reason for this.

show 1 reply