title: Iterate though all possible floating-point values, starting from lowest
body: <p>I am writing a unit test for a math function and I would like to be able to "walk" all possible floats/doubles.</p>
  
  <p>Due to IEEE shenanigans, floating types cannot be incremented (<code>++</code>) at their extremities. See <a href="https://stackoverflow.com/questions/16152536/long-double-increment-operator-not-working-on-large-numbers">this question</a> for more details. That answer states :</p>
  
  <blockquote>
    <p>one can only add multiples of 2^(n-N)</p>
  </blockquote>
  
  <p>But never mentions what little <code>n</code> is.</p>
  
  <p>A solution to iterate all possible values from +0.0 to +infinity is given in this great <a href="https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2/" rel="nofollow noreferrer">blog post</a>. The technique involves using a union with an <code>int</code> to walk the different values of a <code>float</code>. This works due to the following properties explained in the post, though they are only valid for positive numbers.</p>
  
  <blockquote>
    <ol>
    <li>Adjacent floats have adjacent integer representations</li>
    <li>Incrementing the integer representation of a float moves to the next representable float, moving away from zero</li>
    </ol>
  </blockquote>
  
  <p>His solution for +0.0 to +infinity (<code>0.f</code> to <code>std::numeric_limits&lt;float&gt;::max()</code>) :</p>
  
  <pre><code>union Float_t {
      int32_t RawExponent() const { return (i &gt;&gt; 23) &amp; 0xFF; }
      int32_t i;
      float f;
  };
  
  Float_t allFloats;
  allFloats.f = 0.0f;
  while (allFloats.RawExponent() &lt; 255) {
      allFloats.i += 1;
  }
  </code></pre>
  
  <p>Is there a solution for -infinity to +0.0 (<code>std::numeric_limits&lt;float&gt;::lowest()</code> to <code>0.f</code>)?</p>
  
  <p>I've tested <a href="https://en.cppreference.com/w/cpp/numeric/math/nextafter" rel="nofollow noreferrer"><code>std::nextafter</code> and <code>std::nexttoward</code></a> and couldn't get them to work. Maybe this is an MSVC issue?</p>
  
  <p>I would be ok with any sort of hack since this is a unit test. Thanks!</p>