Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate IRR (Internal Rate Return) and NPV programmatically in Objective-C
    primarykey
    data
    text
    <p>I am developing a financial app and require <code>IRR (in-built functionality of Excel)</code> calculation and found such great tutorials in <code>C</code> <a href="http://www.codeproject.com/Tips/461049/Internal-Rate-of-Return-IRR-Calculation?msg=4616844#xx4616844xx" rel="nofollow noreferrer">here</a> and such answer in <code>C#</code> <a href="https://stackoverflow.com/questions/5179866/xirr-calculation/6348722#6348722">here</a>.</p> <p>I implemented code of the C language above, but it gives a perfect result when IRR is in positive. It is not returning a negative value when it should be. Whereas in Excel <code>=IRR(values,guessrate)</code> returns negative IRR as well for some values.</p> <p>I have referred to code in above C# link too, and it seems that it follows good procedures and returns errors and also hope that it returns negative IRR too, the same as Excel. But I am not familiar with C#, so I am not able to implement the same code in Objective-C or C.</p> <p>I am writing C code from the above link which I have implemented for helping you guys.</p> <pre><code>#define LOW_RATE 0.01 #define HIGH_RATE 0.5 #define MAX_ITERATION 1000 #define PRECISION_REQ 0.00000001 double computeIRR(double cf[], int numOfFlows) { int i = 0, j = 0; double m = 0.0; double old = 0.00; double new = 0.00; double oldguessRate = LOW_RATE; double newguessRate = LOW_RATE; double guessRate = LOW_RATE; double lowGuessRate = LOW_RATE; double highGuessRate = HIGH_RATE; double npv = 0.0; double denom = 0.0; for (i=0; i&lt;MAX_ITERATION; i++) { npv = 0.00; for (j=0; j&lt;numOfFlows; j++) { denom = pow((1 + guessRate),j); npv = npv + (cf[j]/denom); } /* Stop checking once the required precision is achieved */ if ((npv &gt; 0) &amp;&amp; (npv &lt; PRECISION_REQ)) break; if (old == 0) old = npv; else old = new; new = npv; if (i &gt; 0) { if (old &lt; new) { if (old &lt; 0 &amp;&amp; new &lt; 0) highGuessRate = newguessRate; else lowGuessRate = newguessRate; } else { if (old &gt; 0 &amp;&amp; new &gt; 0) lowGuessRate = newguessRate; else highGuessRate = newguessRate; } } oldguessRate = guessRate; guessRate = (lowGuessRate + highGuessRate) / 2; newguessRate = guessRate; } return guessRate; } </code></pre> <p>I have attached the result for some value which are different in Excel and the above C language code.</p> <pre><code> Values: Output of Excel: -33.5% 1 = -18.5, Output of C code: 0.010 or say (1.0%) 2 = -18.5, 3 = -18.5, 4 = -18.5, 5 = -18.5, 6 = 32.0 Guess rate: 0.1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload