title: Floating point errors in C++
body: <p>I need to have some float divisions that must be accurate like double version of them. I can change divided value - it represents a mapping and I can offset it - to correct eventual floating point errors.</p>
  <p>To correct the errors, I use following code:</p>
  <pre><code>do 
  {
      float fValue = float(x) / 1024.f;
      double oldFValue = fValue;
      double dValue = double(x) / 1024.0;
      if(oldFValue != dValue)
      {
          x += 1;
      }
      else
      {
          break;
      }
  }while(1);
  </code></pre>
  <p>With this code, for</p>
  <pre><code>x = 11 
  </code></pre>
  <p>I have in debugger (Visual Studio 2010):</p>
  <pre><code>fValue = 0.010742188
  oldFValue = 0.010742187500000000
  </code></pre>
  <p>Can you please explain why double value is different from float value? Is this a debugger problem or a floating point conversion problem? I'm asking this because:</p>
  <pre><code>if(oldFValue != dValue)
  </code></pre>
  <p>is never true, even though it should be. Should I compare the float value with the double value in some other way? I need the result of float division to be exactly same as the double division.</p>