title: Javascript IRR (Internal rate of return) Formula Accuracy
body: <p>I'm using a IRR function in javascript to create calculation a that is done in excel using its own IRR function. The problem is mine is little off and I have no idea why. Here's the code below.</p>
  
  <pre><code>var IRRval = [];
  
  IRRval.push(-financed);
  for (i = 0; i &lt; period; i++) {
      IRRval.push(rental);
  }
  
  var IRR = IRRCalc(IRRval, 0.001) * 0.01;
  
  function IRRCalc(CArray, guest) {
      inc = 0.000001;
      do {
          guest += inc;
          NPV = 0;
          for (var j=0; j &lt; CArray.length; j++) {
              NPV += CArray[j] / Math.pow((1 + guest), j);
          }
      } while (NPV &gt; 0);
      return guest * 100;
  }
  </code></pre>
  
  <p>Now if you use these figures:</p>
  
  <hr>
  
  <p>Period 24</p>
  
  <p>Financed 22000</p>
  
  <p>Rental 1017.5000</p>
  
  <p>My Result is: 0.008523000000000175</p>
  
  <p>Excel Result is: 0.008522918 </p>
  
  <hr>
  
  <p><strong>OR</strong></p>
  
  <hr>
  
  <p>Period 42</p>
  
  <p>Financed 218000</p>
  
  <p>Rental 5917.1429</p>
  
  <p>My Result is: 0.006247000000000489</p>
  
  <p>Excel Result is: 0.00624616</p>
  
  <hr>
  
  <p>The Excel function is called: <code>=IRR(T12:T73,0.01)</code> T12-T73 is the same figures I'm using.</p>
  
  <p>Any help would be much appreciated, Thanks Jason</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've solved it by changing the values below. But now the performance is too slow. Any ideas on how to improve this?</p>
  
  <pre><code>IRRCalc(IRRval, 0.001)
  
  //to
  
  IRRCalc(IRRval, 0.0001)
  
  inc = 0.000001;
  
  //to
  
  inc = 0.00000001;
  </code></pre>