Using floating point numbers as hash keys (2017)
8 days ago
- #hash-tables
- #floating-point
- #programming-languages
- Floating point numbers can be used as hash keys if a suitable comparison operator and hash function are defined.
- Common pitfalls include handling special values like minus zero, infinity, and NaN.
- Floating point keys are error-prone due to rounding and truncation errors, similar to equality comparisons.
- Approximate equality is hard to reconcile with the exact nature of hash functions.
- Floating point numbers are precise in isolation; issues arise from operations like arithmetic or conversions.
- Basic requirements for a floating point hash function include consistent mapping and handling equality.
- Special cases like +0.0 and -0.0 must map to the same hash value.
- NaN values are problematic because NaN != NaN, making them difficult to handle in hash tables.