title: Failure to get next 32 bit float value using the C/C++ nextafter/nexttoward function in <math.h>
body: <p>Using C or C++, I want to increment over the range of all representable 32 bit floating point numbers in a loop, similarly to the way you might increment over all distinct values represented by a 32 bit integer.</p>
  
  <p>Something like:
  for(float f = FLOAT_MIN; f &lt; MAX; f = Next_Float(f))
  {...}</p>
  
  <p>I think that I could use the "nexttoward" or "nextafter" functions in the standard math library to accomplish that task. See <a href="http://www.cplusplus.com/reference/cmath/nextafter/" rel="nofollow">http://www.cplusplus.com/reference/cmath/nextafter/</a></p>
  
  <p>Now, when I test out the "nexttoward" or "nextafter" functions with doubles or long doubles and compiling with g++ 4.7 on Ubuntu 13.04, I don't run in to any problems. See test code:</p>
  
  <pre><code>#include &lt;math.h&gt;   
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  int main ()
  {
      double f = 0.1;
      for(int i = 0; i &lt; 5; ++i)
      {
      //Marginally increment f in the upper direction.
      f = nexttoward(f,999.999);
      std::cout &lt;&lt; std::setprecision(70) &lt;&lt; f &lt;&lt; std::endl;
      std::cout &lt;&lt; nexttoward(f,999.999) &lt;&lt; std::endl;
      }
    return 0;
  }
  </code></pre>
  
  <p>The program's floating point output values increase steadily as expected:</p>
  
  <p>ubuntu@ubuntu:~$ g++ -o temp ~/temp.cpp</p>
  
  <p>ubuntu@ubuntu:~$ ./temp</p>
  
  <p>0.10000000000000001942890293094023945741355419158935546875
  0.100000000000000033306690738754696212708950042724609375
  0.100000000000000033306690738754696212708950042724609375
  0.10000000000000004718447854656915296800434589385986328125
  0.10000000000000004718447854656915296800434589385986328125
  0.1000000000000000610622663543836097232997417449951171875
  0.1000000000000000610622663543836097232997417449951171875
  0.10000000000000007494005416219806647859513759613037109375
  0.10000000000000007494005416219806647859513759613037109375
  0.100000000000000088817841970012523233890533447265625</p>
  
  <p>ubuntu@ubuntu:~$</p>
  
  <p>But when I try using floats instead of doubles, the "nexttoward" and "nextafter" functions fail me - the functions appear to return values of greater precision than 32 bit floats, and when I assign the return values to my 32-bit float, the float retain its original value rather than going up to the next higher value. See example code and output:</p>
  
  <pre><code>#include &lt;math.h&gt;    
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  int main ()
  {
      float f = 0.1f;
      for(int i = 0; i &lt; 10; ++i)
      {
      //Marginally increment f in the upper direction.
      f = nexttoward(f,999.999f);
      std::cout &lt;&lt; std::setprecision(70) &lt;&lt; f &lt;&lt; std::endl;
      std::cout &lt;&lt; nexttoward(f,999.999f) &lt;&lt; std::endl;
      }
    return 0;
  }
  </code></pre>
  
  <p>Notice that the second output value from "nexttoward" is of greater precision and that f maintains the same value: </p>
  
  <p>ubuntu@ubuntu:~$ g++ -o temp ~/temp.cpp</p>
  
  <p>ubuntu@ubuntu:~$ ./temp</p>
  
  <p>0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625
  0.100000001490116119384765625
  0.10000000149011613326255343281445675529539585113525390625</p>
  
  <p>I want to increment over all 32-bit floating point values, not all 64-bit double precision values - the time to increment over all the double precision values would be too long.</p>
  
  <p>How do I fix this problem and achieve an efficient, convenient, and portable way to iterate over the range of a 32-bit floating point variable? </p>