title: If operator< works properly for floating-point types, why can't we use it for equality testing?
body: <p>Properly testing two floating-point numbers for equality is something that a lot of people, including me, don't fully understand. Today, however, I thought about how some standard containers define equality in terms of <code>operator&lt;</code>. I always see people with problems surrounding equality, but never with the other relational comparisons. There are even <a href="http://en.cppreference.com/w/cpp/numeric/math/isless" rel="noreferrer">silent versions</a> of them to use, which include everything except for equality and inequality.</p>
  
  <p>Assuming <code>operator&lt;</code> works "properly", unlike <code>operator==</code>, why couldn't we do this:</p>
  
  <pre><code>bool floateq(float a, float b) {
      //check NaN
      return !(a &lt; b) &amp;&amp; !(b &lt; a);
  }
  </code></pre>
  
  <p>In fact, I did run a test with an additional overload for doubles, as seen <a href="http://coliru.stacked-crooked.com/view?id=c0343d114456b98619eba0985a6cc758-3b440a87a52fe2ae7c853c82f4c5144f" rel="noreferrer">here</a>, and it seems to have the same pitfalls as comparing them with <code>operator==</code>:</p>
  
  <pre><code>std::cout &lt;&lt; "float-&gt;double vs double: " 
            &lt;&lt; floateq(static_cast&lt;double&gt;(0.7f), 0.7) &lt;&lt; " " 
            &lt;&lt; (static_cast&lt;double&gt;(0.7f) == 0.7) &lt;&lt; "\n";
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>float->double vs double: 0 0</p>
  </blockquote>
  
  <p>Am I to worry about using <em>all</em> comparison operators, or is there some other aspect of comparing floating-point numbers that I'm not understanding correctly?</p>