cc [ flags ] -I/usr/local/include file(s) -L/usr/local/lib -lmcw [ ... ] #include <mathcw.h> extern float compoundf (float rate, float num_periods); extern double compound (double rate, double num_periods); extern long double compoundl (long double rate, long double num_periods); extern __float80 compoundw (__float80 rate, __float80 num_periods); extern __float128 compoundq (__float128 rate, __float128 num_periods); extern long_long_double compoundll (long_long_double rate, long_long_double num_periods); extern decimal_float compounddf (decimal_float rate, decimal_float num_periods); extern decimal_double compoundd (decimal_double rate, decimal_double num_periods); extern decimal_long_double compounddl (decimal_long_double rate, decimal_long_double num_periods); extern decimal_long_long_double compounddll (decimal_long_long_double rate, decimal_long_long_double num_periods);
NB: Functions with prototypes containing underscores in type names may be available only with certain extended compilers.
The last formula is numerically stable, and is used for the computation.compound(r,n) = (1 + r)**n [where r > -1] = exp(log((1 + r)**n)) = exp(n * log(1 + r)) = exp(n * log1p(r))
For example, a principal amount P invested at 5% annual interest and compounded weekly for 30 years would yield
The yield with monthly compounding isPw = P * compound(0.05 / 52, 30 * 52) ~= 4.478 P
The yield with quarterly compounding isPm = P * compound(0.05 / 12, 30 * 12) ~= 4.467 P
The yield with annual compounding ispq = P / compound(0.10 / 4, 30 * 4) ~= 4.440 P
Pa = P * compound(0.05 , 30) ~= 4.322 P
With instantaneous compounding, take the limit of arbitrarily large t in compound(r / t, 30 * t) to get a value of about 4.482 P when the annual rate is 5%.