Let's say we have the following python class (the problem exists in Java in the same way with
class temperature: def __init __ (auto, degrees): self.degrees = degrees
degrees is the temperature in Kelvin as a float. Now, I would like to implement equality and hash tests for
Temperature so that
- compares the floats up to an epsilon difference instead of a direct equality test,
- and honors the contract that
a == binvolved
hash (a) == hash (b).
def __eq __ (self, other): return abs (self.degrees - other.degrees) <EPSILON def __hash __ (auto): return # What's going on here?
The python documentation talks a bit about hashing numbers to make sure that
hash (2) == hash (2.0) but it's not quite the same problem.
Am I even on the right track? And if so, what is the standard way to implement hashing in this situation?