|
|
|
|
|
by thaumasiotes
208 days ago
|
|
That won't work; as integers, 100.02 and 99.997 are unequal, but 1.0002 and 0.99997 are equal at 0.01 precision. (And indeed also equal at 0.001 precision!) You'd need to round. I had the impression that the usual way to compare floats is to define a precision and check for -p < (a - b) < p. In this case 0.99997 - 1.0002 = -0.00023, which correctly tells us that the two numbers are equal at 0.001 precision and unequal at 0.0001. |
|
You can do it if you produce two hash values for each key (and clean up your duplicates later), but not if you produce only one.
Of course most of the time if you are doing equality comparisons on floats you have a fundamental conceptual problem with your code.