title: Javascript adding 0.1 to 0.2 causes 0.30000000000000004
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3556789/javascript-math-error-inexact-floats">Javascript Math Error: Inexact Floats</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following code to make an animation (doIt does the animation but not related). </p>
  
  <pre><code>function recur(max, i){
      console.log("i: " + i);
      if ( i &lt;= 1){
          setTimeout(function(){
              // doIt(max,i);
              recur(max, i + 0.1);                
          },100);
      } else {
          // OK
      }
  }
  recur(16,0);
  </code></pre>
  
  <p>However, the i values are not consistent. For the following code the output is (Google Chrome 20):</p>
  
  <pre><code>i: 0 
  i: 0.1 
  i: 0.2 
  i: 0.30000000000000004
  i: 0.4 
  i: 0.5 
  i: 0.6 
  i: 0.7 
  i: 0.7999999999999999 
  i: 0.8999999999999999 
  i: 0.9999999999999999 
  i: 1.0999999999999999 
  </code></pre>
  
  <p>Why is this happening? I want 0.3 not a so close number. Unfortunately this happens in every iteration.</p>