title: JavaScript parseFloat error: 1.445 + 2 = 3.4450000000000003
body: <p>I encountered this weird anomaly creating a simple javaScript demo that I am going to use to teach my 11 year old the basics of coding. I think that perhaps I didn't sanitize the input strings before I used the javaScript parse float javaScript function. The program works fine for other floating point decimals ie. 2.5 + 3 = 5.5. However in this one instance 1.445 + 2 = 3.4450000000000003. Purely out of curiosity I would like to know what is going on here. So with out any further delay, here's the JavaScript code:</p>
  
  <pre><code>var funct = null;
  function clarifyOperation(x)
  {
      if(x=="add")
      {
          document.getElementById("operation").innerHTML="&amp;nbsp;&amp;#43;&amp;nbsp;";
          funct="add";
      }
      else if(x=="subtract")
      {
          document.getElementById("operation").innerHTML="&amp;nbsp;&amp;#45;&amp;nbsp;";
          funct="subtract";
      }
      else if(x=="multiply")
      {
          document.getElementById("operation").innerHTML="&amp;nbsp;&amp;#215;&amp;nbsp;";
          funct="multiply";
      }
      else if(x=="divide")
      {        
          document.getElementById("operation").innerHTML="&amp;nbsp;&amp;#247;&amp;nbsp;";
          funct="divide";
      }
  }
  
  function solve()
  {
      var x=document.getElementById("inputX").value;
      var y=document.getElementById("inputY").value;
      var z;
      if(funct=="add")
      {
          z=parseFloat(x)+parseFloat(y);
          document.getElementById("result").value=z;
      }
      else if(funct=="subtract")
      {
          z=parseFloat(x)-parseFloat(y);
          document.getElementById("result").value=z;
      }
      else if(funct=="multiply")
      {
          z=parseFloat(x)*parseFloat(y);
          document.getElementById("result").value=z;
      }
      else if(funct=="divide")
      {
          z=parseFloat(x)/parseFloat(y);
          document.getElementById("result").value=z;
      }
  }
  </code></pre>
  
  <p>Here is the HTML code: </p>
  
  <pre><code>&lt;input type="text" value="" id="inputX"/&gt;
  &lt;span id="operation"&gt;&amp;nbsp;&lt;/span&gt;
  &lt;input id="inputY" type="text" value="" /&gt;
  &lt;span&gt;&amp;nbsp;&amp;#61;&amp;nbsp;&lt;/span&gt;
  &lt;input type="text" value="" id="result" /&gt;&lt;br /&gt;
  &lt;input type="radio" name="operation" value="add" onchange="clarifyOperation(this.value)" /&gt;
  &lt;span&gt;&amp;#43;&lt;/span&gt;&lt;br /&gt;
  &lt;input type="radio" name="operation" value="subtract" onchange="clarifyOperation(this.value)" /&gt;
  &lt;span&gt;&amp;#45;&lt;/span&gt;&lt;br /&gt;
  &lt;input type="radio" name="operation" value="multiply" onchange="clarifyOperation(this.value)" /&gt;
  &lt;span&gt;&amp;#215;&lt;/span&gt;&lt;br /&gt;
  &lt;input type="radio" name="operation" value="divide" onchange="clarifyOperation(this.value)" /&gt;
  &lt;span&gt;&amp;#247;&lt;/span&gt;&lt;br /&gt;
  &lt;input type="button" value="calculate" onclick="solve()" /&gt;
  &lt;hr /&gt;
  </code></pre>
  
  <p>Reminder to the community: keep your comment's useful. </p>