title: IEqualityComparer<double> with a tolerance; how to implement GetHashCode?
body: <p>I'm implementing a reusable DoubleEqualityComparer (with a custom tolerance: the "epsilon" constructor parameter) to ease the usage of LINQ with sequences of double. For example:</p>
  
  <pre><code>bool myDoubleFound = doubles.Contains(myDouble, new DoubleEqualityComparer(epsilon: 0.01));
  </code></pre>
  
  <p>What is the right way to implement GetHashCode? Here's the code:</p>
  
  <pre><code>   public class DoubleEqualityComparer : IEqualityComparer&lt;double&gt;, IEqualityComparer&lt;double?&gt;
      {
          private readonly double epsilon;
  
          public DoubleEqualityComparer(double epsilon)
          {
              if (epsilon &lt; 0)
              {
                  throw new ArgumentException("epsilon can't be negative", "epsilon");
              }
  
              this.epsilon = epsilon;
          }
  
          public bool Equals(double x, double y)
          {
              return System.Math.Abs(x - y) &lt; this.epsilon;
          }
  
          public int GetHashCode(double obj)
          {
              // ?
          }
     }
  </code></pre>
  
  <p>PS: I can always return the same value (ex: GetHashCode(double obj){ return 0; }) to always force the call to Equals(double, double) method (not very performant, I know), but I remember that this solution causes problems when the comparer is used with a dictionary...</p>