Monday, November 19, 2012

Time Value of Money

My accounting book had two tables that we used to perform some functions to evaluate investments and to get rates of return.  I was curious about how to generate those tables and the magic behind the functions built into financial calculators.  So I looked up the math behind the tables and wrote a couple of small programs to create the tables.

I found the formula for present value from this web site:  http://www.financeformulas.net/Net_Present_Value.html   The code I wrote for this function is as follows:


#include <stdio.h>
#include <math.h>

int
main ()
{

  int timelength = 40;
  int t = 1;
  double r;

  printf ("Net Present Value.\n");
  for (t = 4; t < 26; t++)
    printf ("\t%d%%", t);
  printf ("\n");

  for (t = 1; t <= timelength; t++)
    {
      printf ("%d\t", t);
      for (r = 4; r < 26; r++)
        printf ("%f\t", 1 / pow (1.0 + r / 100, t));
      printf ("\n");
    }
}
 
to compile this you need to use the -lm flag
 
gcc -lm filename.c -o npv 

The output is tab separated and  can easily be imported into a spreed sheet program and formatted.

 

We also had a table to calculate the future return on an Annuity, and I found the way to calculate the table with the formula on this page: http://www.financeformulas.net/Present_Value_of_Annuity.html

#include <stdio.h>
#include <math.h>

int
main ()
{
  int timelength = 40;
  int t = 1;
  double r;

  printf ("Present Value of an Annuity.\n");
  for (t = 4; t < 26; t++)
    printf ("\t%d%%", t);
  printf ("\n");

  for (t = 1; t <= timelength; t++)
    {
      printf ("%d\t", t);
      for (r = 4; r < 26; r++)
        printf ("%f\t", (1 - pow (1.0 + r / 100, -t)) / (r / 100));
      printf ("\n");
    }
}

to compile this you need to use the -lm flag
 
gcc -lm filename.c -o apv 

The text output that this generates looks like:

 

No comments:

Post a Comment