#include <stdio.h>
#include <math.h>
#include <sys/time.h>
double pv, fv, rate, years, periods ;
double get_years() {
return log(fv/pv) / (periods * log(1.0 + (rate/periods))) ;
}
double get_cont_years() {
return log( fv / pv) / rate ;
}
double get_rate() {
return periods * ( pow( ( fv / pv), 1.0 / (periods * years) ) - 1.0 ) ;
}
double get_cont_rate() {
return log( fv / pv) / years ;
}
double get_fv() {
return pv * pow(1 + (rate / periods), years * periods) ;
}
double get_cont_fv() {
return pv * exp(rate * years) ;
}
double get_pv() {
return (fv / pow( (1.0 + (rate / periods)) , periods * years) ) ; // (exp(rate * years * periods) ) ) ;
}
double get_cont_pv() {
return (fv / (exp(rate * years) ) ) ;
}
void report(double n ) {
periods = n ;
printf("FV : Rate=%lf, PV=%03.2lf, Years=%lf, Periods=%d, FV=%0.2lf\n", rate, pv, years, (int) periods, get_fv() );
printf("YEARS : Rate=%lf, PV=%03.2lf, Years=%lf, Periods=%d, FV=%0.2lf\n", rate, pv, get_years(), (int)periods, fv );
printf("PV : Rate=%lf, PV=%03.2lf, Years=%lf, Periods=%d, FV=%0.2lf\n", rate, get_pv(), years, (int)periods, fv );
printf("RATE : Rate=%lf, PV=%03.2lf, Years=%lf, Periods=%d, FV=%0.2lf\n", get_rate(), pv, years, (int)periods, fv );
printf("\n") ;
}
void report_e() {
printf("FV : Rate=%lf, PV=%03.2lf, Years=%lf, FV=%0.2lf\n", rate, pv, years, get_cont_fv() );
printf("YEARS : Rate=%lf, PV=%03.2lf, Years=%lf, FV=%0.2lf\n", rate, pv, get_cont_years(), fv );
printf("PV : Rate=%lf, PV=%03.2lf, Years=%lf, FV=%0.2lf\n", rate, get_cont_pv(), years, fv );
printf("RATE : Rate=%lf, PV=%03.2lf, Years=%lf, FV=%0.2lf\n", get_cont_rate(), pv, years, fv );
printf("\n") ;
}
int main (int argc, const char * argv[]) {
int rc ;
struct timeval tv_start, tv_end ;
struct timezone tz ;
rc = gettimeofday(&tv_start, &tz) ;
if (rc) {
printf("gettimeofday() failed, rc = %d\n", rc) ;
return rc ;
}
pv = 100.0 ;
fv = 110.0 ;
rate = .10 ;
years = 1.0 ;
periods = 12.0 ;
printf("Compounded Yearly\n") ;
report(1.0) ;
printf("Compounded Semi Anuallly\n") ;
report(2.0) ;
printf("Compounded Quarterly\n") ;
report(4.0) ;
printf("Compounded Monthly\n") ;
report(12.0) ;
printf("Compounded Daily\n") ;
report(365.0) ;
printf("Compounded Hourly\n") ;
report(365.0 * 24.0) ;
printf("Compounded Continuously\n") ;
report_e() ;
// getchar() ;
rc = gettimeofday(&tv_end, &tz) ;
if (rc) {
printf("gettimeofday() failed, rc = %d\n", rc) ;
return rc ;
}
printf("tv_start = %ld.%ld, tv_end = %ld.%ld\n", tv_start.tv_sec, tv_start.tv_usec, tv_end.tv_sec, tv_end.tv_usec) ;
if ( tv_end.tv_usec < tv_start.tv_usec) {
// printf("tv_start_usec - tv_end_usec = %d\n", tv_start.tv_usec - tv_end.tv_usec) ;
tv_end.tv_sec-- ; // borrow from seconds
tv_end.tv_usec += 1000000 ; // a usec is 1 milllionth of a second.
}
printf("Elapsed Time (seconds) was %ld.%06ld\n", (tv_end.tv_sec- tv_start.tv_sec), (tv_end.tv_usec - tv_start.tv_usec)) ;
return 0;
}