title: How to properly compare an integer and a floating-point value?
body: <p>How do I compare an integer and a floating-point value <em>the right way™</em>?</p>
  
  <p>The builtin comparsion operators give incorrect results in some edge cases, for example:</p>
  
  <pre><code>#include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      long long a = 999999984306749439;
      float     b = 999999984306749440.f; // This number can be represented exactly by a `float`.
  
      std::cout &lt;&lt; std::setprecision(1000);
      std::cout &lt;&lt; a &lt;&lt; " &lt; " &lt;&lt; b &lt;&lt; " = " &lt;&lt; (a &lt; b) &lt;&lt; '\n';
      // Prints `999999984306749439 &lt; 999999984306749440 = 0`, but it should be `1`.
  }
  </code></pre>
  
  <p>Apparently, the comparsion operators convert both operands to a same type before actually comparing them. Here lhs gets converted to <code>float</code>, which causes a loss of precision, and leads to an incorrect result.</p>
  
  <p>Even though I understand what's going on, I'm not sure how to work around this issue.</p>
  
  <hr>
  
  <p><sup>Disclaimer: The example uses a <code>float</code> and a <code>long long</code>, but I'm looking for a generic solution that works for every combination of an integral type and a floating-point type.</sup></p>