I am developing a financial application and need to calculate IRR (in-built functionality of Excel) and found such wonderful tutorials in C here and such an answer in C# here .
I have implemented the C language code above, but it gives the perfect result when the IRR is positive. It does not return a negative value when it should be. If Excel =IRR(values,guessrate) also returns a negative IRR for some values.
I also referenced the code above, the C # link, and it seems that it follows good procedures and returns errors, and also hopes that it will return a negative IRR as well as Excel. But I am not familiar with C #, so I cannot implement the same code in Objective-C or C.
I am writing C code from the link above that I implemented to help you guys.
#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<MAX_ITERATION; i++) { npv = 0.00; for (j=0; j<numOfFlows; j++) { denom = pow((1 + guessRate),j); npv = npv + (cf[j]/denom); } /* Stop checking once the required precision is achieved */ if ((npv > 0) && (npv < PRECISION_REQ)) break; if (old == 0) old = npv; else old = new; new = npv; if (i > 0) { if (old < new) { if (old < 0 && new < 0) highGuessRate = newguessRate; else lowGuessRate = newguessRate; } else { if (old > 0 && new > 0) lowGuessRate = newguessRate; else highGuessRate = newguessRate; } } oldguessRate = guessRate; guessRate = (lowGuessRate + highGuessRate) / 2; newguessRate = guessRate; } return guessRate; }
I attached the result for some value that is different in Excel and the above C 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
c ios objective-c iphone excel
Rahul patel
source share