title: about float number comparision in C++
body: <p>I have a loop to loop a floating number between given min and max range as follow</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  void main(void)
  {
    for (double x=0.012; x&lt;=0.013; x+=0.001) 
    {
      cout &lt;&lt; x &lt;&lt; endl;
    }
  }
  </code></pre>
  
  <p>It is pretty simple code but as I know in computer language, we need to compare two floating numbers with EPS considered. Hence, above code doesn't work (we expect it to loop two times from 0.012 to 0.013 but it only loop once). So I manually add an EPS to the upper limit. </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #define EPS 0.0000001
  
  void main(void)
  {
    for (double x=0.012; x&lt;=0.013+EPS; x+=0.001) 
    {
      cout &lt;&lt; x &lt;&lt; endl;
    }
  }
  </code></pre>
  
  <p>and it works now. But it looks ugly to do that manually since EPS should really depends on machine. I am porting my code from matlab to C++ and I don't have problem in matlab since there is eps command. But is there anything like that in C/C++?</p>