Click here to Skip to main content
15,880,392 members
Articles / General Programming / Algorithms
Tip/Trick

Internal Rate of Return (IRR) Calculation

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
18 Sep 2012CPOL2 min read 85.8K   8   9
Program to calculate the IRR value using C/C++ similar to the one available in Excel

Introduction

IRR (Internal Rate of Return) is the most widely used financial indicator while assessing return on an investment or a project. It is defined as the discount rate which makes the net present value of the cash flows from the investment equal to zero. In Excel, we have IRR formula to compute the discount rate based on the cashflows for an investment/project. This program is written in C/C++ and provided the IRR rate to an accuracy of upto 6 decimal points.

Background

IRR Rate is mathematically derived by assuming a rate and computing the formula so that the value becomes zero. I am providing an example to understand the formula used:

Year/Cash Flow
0/-4000
1/1200
2/1410
3/1875
4/1050

The IRR rate (r) for this cash flow is given by the formula:

\mathrm{NPV} = -4000+\frac{1200}{(1+r)^1} + \frac{1410}{(1+r)^2} + \frac{1875}{(1+r)^3} + \frac{1050}{(1+r)^4} = 0.

Here, we need to assume the rate r and find out an optimum rate for which the NPV (Net Present Value) is zero. The program uses the same logic to calculate the IRR.

Using the Code

For IRR calculation, we need the following inputs:

  1. The cash flows with which we need to compute the IRR
  2. The period for which we are computing the IRR for the above cashflows

With these inputs, we compute the NPV value and check if it is zero or to the nearest precision possible. The following parameters are used for the computation:

  • LOW_RATE: The initial rate with which we compute the NPV. This is # defined as 0.01 (1%)
  • HIGH_RATE: The highest rate upto which we should consider for computing NPV. This is # defined as .5 (50%)
  • MAX_ITERATION: There is always a possibility of not being able to arrive at the rate for certain cashflows. This variable acts as a stopper for the number of iterations the code should check for NPV so as to ensure that the program doesn't go for an infinite loop.
  • PRECISION_REQ: NPV value will not normally hit zero. We can find the NPV value with a precision upto a certain value. This is set as 0.00000001. Hence when the computed NPV is below this value, the calculation stops and the rate used will be the IRR.

Every time the new rate is arrived as an average rate of LOW and HIGH rate. Depending upon the NPV value, the LOW and HIGH rate are updated every time to drill down to IRR rate.

Following is the code snippet of the function computing IRR:

C++
#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;
}
C++
//Call to the above function in main method
int main()
{
 //Cash flows
 double cf[30], irr = 0.00;
 int numOfFlows;
 cf[0] = -70000;
 cf[1] = 12000;
 cf[2] = 15000;
 cf[3] = 18000;
 cf[4] = 21000;
 cf[5] = 26000;
 numOfFlows = 6;
 irr = computeIRR(cf, numOfFlows);
 printf("\nFinal IRR: %.8f", irr);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank you. Pin
Pelli25-Oct-21 3:41
Pelli25-Oct-21 3:41 
QuestionInternal Rate of Return with Newton method / Kotlin Pin
Member 1488706111-Jul-20 12:19
Member 1488706111-Jul-20 12:19 
QuestionResults quite different from Excel Pin
Member 136041832-Jan-18 19:39
Member 136041832-Jan-18 19:39 
Questionpow Pin
Girish CSC14-Apr-17 20:02
Girish CSC14-Apr-17 20:02 
Questione.gNice work but still need some impovization Pin
rahul.peerbits19-Jul-13 19:52
rahul.peerbits19-Jul-13 19:52 
QuestionThanks for the article Pin
Bilaltm17-Oct-12 6:31
Bilaltm17-Oct-12 6:31 
AnswerRe: Thanks for the article Pin
Member 1455615110-Aug-19 21:40
Member 1455615110-Aug-19 21:40 
QuestionMax iterations Pin
YvesDaoust24-Sep-12 8:58
YvesDaoust24-Sep-12 8:58 
GeneralMy vote of 3 Pin
YvesDaoust24-Sep-12 8:53
YvesDaoust24-Sep-12 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.