Home Segments Index Top Previous Next

117: Mainline

Still another virtue of procedure abstraction is that you can change easily the way that a computation is done. If you decide, for example, to replace the 72.0 / r approximation by an exact computation, you can redefine doubling_time easily to accommodate the change using the following exact formula:

t = log 2.0 / log (1.0 + r/100)

Note that the exact formula requires a function, log, drawn from C's mathematics library, as indicated by the inclusion of the #include <math.h> line:

#include  
... 
double doubling_time (double r) { 
  double result = log (2.0) / log (1.0 + r / 100.0); 
  printf ("The doubling time for rate %f is exactly %f years.\n", 
                                      r,            result); 
  return result; 
} 
...