title: How to loop over exact representations of floating point numbers?
body: <p>I am trying to loop exactly from one floating point number to the next. Say, I need to loop from <code>std::numeric_limits&lt;float&gt;::epsilon()</code> to <code>1</code>, which are both exactly representable IEEE754 numbers. My code is:</p>
  
  <pre><code> using nld = std::numeric_limits&lt;float&gt;;
   auto h = nld::epsilon();
   for (; h &lt; 1; h = std::nextafter(h, 1)) {
       std::cerr &lt;&lt; "h: " &lt;&lt; h &lt;&lt; std::endl;
   }
  </code></pre>
  
  <p>which loops indefinitely beacuse h is exactly representable, so <code>nextafter</code> keeps returning it. I also know that adding machine epsilon to h in a loop will not cut it: floating point numbers are not equally spaced. How do I loop over the exact representations of IEEE754 numbers?</p>
  
  <p>The <code>not equally spaced</code> problem presents itself here:</p>
  
  <pre><code> using nld = std::numeric_limits&lt;float&gt;;
   auto h = nld::epsilon();
   for (; h &lt; 4; h += nld::epsilon()) {
       if (h = h + nld::epsilon()) {
           std::cerr &lt;&lt; "h: " &lt;&lt; h &lt;&lt; std::endl;
       }
   }
  </code></pre>
  
  <p>which keeps printing <code>2</code> for me</p>