title: Finding the smallest integer that can not be represented as an IEEE-754 32 bit float
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-ex">Which is the first integer that an IEEE 754 float is incapable of representing exactly?</a>  </p>
  </blockquote>
  
  
  
  <p>Firstly, this IS a homework question, just to clear this up immediately. I'm not looking for a spoon fed solution of course, just maybe a little pointer to the right direction.</p>
  
  <p>So, my task is to find the smallest positive integer that can not be represented as an IEEE-754 float (32 bit). I know that testing for equality on something like "5 == 5.00000000001" will fail, so I thought I'd simply loop over all the numbers and test for that in this fashion:</p>
  
  <pre><code>int main(int argc, char **argv)
  {
      unsigned int i; /* Loop counter. No need to inizialize here. */
  
      /* Header output */
      printf("IEEE floating point rounding failure detection\n\n");
  
      /* Main program processing */
      /* Loop over every integer number */
      for (i = 0;; ++i)
      {
          float result = (float)i;
  
          /* TODO: Break condition for integer wrapping */
  
          /* Test integer representation against the IEEE-754 representation */
          if (result != i)
              break; /* Break the loop here */
      }
  
      /* Result output */
      printf("The smallest integer that can not be precisely represented as IEEE-754"
             " is:\n\t%d", i);
  
  
      return 0;
  }
  </code></pre>
  
  <p>This failed. Then I tried to subtract the integer "i" from the floating point "result" that is "i" hoping to achieve something of a "0.000000002" that I could try and detect, which failed, too.</p>
  
  <p>Can someone point me out a property of floating points that I can rely on to get the desired break condition?</p>
  
  <p>-------------------- Update below ---------------</p>
  
  <p>Thanks for help on this one! I learned multiple things here:</p>
  
  <ol>
  <li><p>My original thought was indeed correct and determined the result on the machine it was intended to be run on (Solaris 10, 32 bit), yet failed to work on my Linux systems (64 bit and 32 bit).</p></li>
  <li><p>The changes that Hans Passant added made the program also work with my systems, there seem to be some platform differences going on here that I didn't expect,</p></li>
  </ol>
  
  <p>Thanks to everyone!</p>