title: C# Compare two double with .Equals()
body: <p>I use ReShaper and when I compare two double values with ==, it suggests that I should use the Math. ABS method with a tolerance. See: <a href="https://www.jetbrains.com/help/resharper/2016.2/CompareOfFloatsByEqualityOperator.html" rel="noreferrer">https://www.jetbrains.com/help/resharper/2016.2/CompareOfFloatsByEqualityOperator.html</a></p>
  
  <p>This example</p>
  
  <pre><code>double d = 0.0;
  double d2 = 0.0;
  if (d == d2)
  {
      /* some code */
  }
  </code></pre>
  
  <p>is then converted to</p>
  
  <pre><code>double d = 0.0;
  double d2 = 0.0;
  if (Math.Abs(d - d2) &lt; TOLERANCE)
  {
      /* some code */
  }
  </code></pre>
  
  <p>But I think it's really complicated for a developer to think about the right tolerance. So I thought this may be implemented in the Double.Equals() method.</p>
  
  <p>But this method is implemented like so</p>
  
  <pre><code>public override bool Equals(Object obj) {
      if (!(obj is Double)) { 
          return false;
      } 
      double temp = ((Double)obj).m_value; 
      // This code below is written this way for performance reasons i.e the != and == check is intentional.
      if (temp == m_value) { 
          return true;
      }
      return IsNaN(temp) &amp;&amp; IsNaN(m_value);
  }
  
  public bool Equals(Double obj)
  { 
      if (obj == m_value) {
          return true; 
      } 
      return IsNaN(obj) &amp;&amp; IsNaN(m_value);
  }
  </code></pre>
  
  <p><strong>Why is that? And what is the correct way to compare double values?</strong></p>