title: How do you find a float's nearest non-equal value?
body: <p>A <code>float</code> (a.k.a. single) value is a 4-byte value, and supposed to represent any real-valued number. Because of the way it is formatted and the finite number of bytes it is made off, there is a minimum value and a maximum value it can represent, and it has a finite precision, depending on it's own value. </p>
  
  <p>I would like to know if there is a way to get the closest possible value above or below some reference value, given the finite precision of a float. With integers, this is trivial: one simply adds or subtracts 1. But with a <code>float</code>, you can't simply add or subtract the minimum float value and expect it to be different from your original value. I.e.</p>
  
  <pre><code>float FindNearestSmaller (const float a)
  {
      return a - FLT_MIN; /* This doesn't necessarily work */
  }
  </code></pre>
  
  <p>In fact, the above will almost never work. In the above case, the return will generally still equal <code>a</code>, as the <code>FLT_MIN</code> is far beyond the precision of <code>a</code>. You can easily try this out for yourself: it works for e.g. <code>0.0f</code>, or for very small numbers of order <code>FLT_MIN</code>, but not for anything between 0 and 100.</p>
  
  <p>So how would you get the value that is closest but smaller or larger than <code>a</code>, given floating point precision?</p>
  
  <p><strong>Note:</strong> Though i am mainly interested in a C/C++ answer, I assume the answer will be applicable for most programming languages.</p>