In my scenario, two floating point numbers (say double
s) are considered equal if their absolute difference is within a certain range. This means it's easy to use double
s as keys for a std::map
: I just need to define a custom comparison function. But I'm not sure what's a good approach for std::unordered_map
, which needs a hash function in addition to a comparison function.
I don't think there is any method to produce the same hash value for two close double
s in general, as that would allow us to keep incrementing one of them in small steps, eventually obtaining two very different double
s with the same hash value.
Maybe there is a way to "normalize" floating point numbers to some "standard" values? For example, one very bad way to do it (that doesn't even always work) would be to use the hash of the nearest integer.
If std::unordered_map
is just not the right choice, I'm curious what other choices I have for O(1) associative containers with floating point numbers as keys. (Note that the keys have to be floating point numbers, not custom high-precision decimal numbers, for performance reason.)