title: C++ floating point precision
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples">Floating point inaccuracy examples</a>  </p>
  </blockquote>
  
  
  
  <pre><code>double a = 0.3;
  std::cout.precision(20);
  std::cout &lt;&lt; a &lt;&lt; std::endl;
  </code></pre>
  
  <p>result: 0.2999999999999999889</p>
  
  <pre><code>double a, b;
  a = 0.3;
  b = 0;
  for (char i = 1; i &lt;= 50; i++) {
    b = b + a;
  };
  std::cout.precision(20);
  std::cout &lt;&lt; b &lt;&lt; std::endl;
  </code></pre>
  
  <p>result: 15.000000000000014211</p>
  
  <p>So.. 'a' is smaller than it should be.
  But if we take 'a'  50 times - result will be bigger than it should be. </p>
  
  <p>Why is this? 
  And how to get correct result in this case?</p>