Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the default hash function for floats/doubles is not very good. #538

Open
nicebyte opened this issue Aug 27, 2024 · 1 comment
Open

the default hash function for floats/doubles is not very good. #538

nicebyte opened this issue Aug 27, 2024 · 1 comment

Comments

@nicebyte
Copy link

the default hash for floats works by casting the value to size_t:

	template <> struct hash<float>
		{ size_t operator()(float val) const { return static_cast<size_t>(val); } };

this is quite bad because anything that is between two integers gets hashed to the same value. my hash table essentially turned into a linear array because most of the values happened to be in the [0; 1] range.

the same applies to hashes for other floating point types (double, long double etc.).

when fixing this, keep in mind that floating point numbers have two representations for 0 (0 and -0) - those two bit patterns should hash to the same value because they represent the same number.

@SirNate0
Copy link

Counterpoint. -0 and 0 are not the same value: They have different behavior when you, for example, do 1.0/-0.0 vs 1.0/+0.0 (giving negative vs positive infinity). But since -0.0==+0.0 (in IEEE 754 at least), it does seem reasonable to make them hash to the same value. Though since NAN == NAN is false, but hash(NAN) == hash(NAN) would be true, there will always be at least that case* where (hash(x) == hash(y)) != (x == y), so it doesn't seem too ridiculous to have hash(-0.0) != hash(+0.0). In other words, replacing the static_cast with a bit_cast doesn't seem too unreasonable.

* Actually many cases when you consider there are many possible bit values for NAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants