home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Developer University / DUProjects / Finance Source / Finance.cpp next >
Encoding:
Text File  |  1996-08-16  |  4.4 KB  |  156 lines  |  [TEXT/CWIE]

  1. //    Copyright 1995 © David A. Wilson. All rights reserved.
  2.  
  3. //====================================================================
  4. #ifndef _FINANCE_
  5.     #include "Finance.h"
  6. #endif
  7.  
  8. #ifndef __FP__
  9.     #include <fp.h>        // PowerPC math functions
  10. #endif
  11.  
  12. //====================================================================
  13. // Savings functions
  14. double    
  15. FutureValue(double presentValue,
  16.             double annualInterestPercent, 
  17.             short compoundsPerYear, 
  18.             double years)
  19. {
  20.     // H-P 35 Math Pac, p 96.
  21.     short    n = compoundsPerYear * years;
  22.     double     i = (annualInterestPercent/100.0) / compoundsPerYear;
  23.     double  factor = pow(1.0 + i, n);
  24.     return presentValue * factor;
  25. }
  26.  
  27. //-------------------------------------------------------------------
  28. double    
  29. FutureValueWithPayments(double payment,
  30.                         short paymentsPerYear,
  31.                         short compoundsPerYear, 
  32.                         double annualInterestPercent, 
  33.                         double years)
  34. {
  35.     // H-P 67/97 Business Decisions Pac, p. B-03
  36.     double     Q;
  37.     double     n;
  38.     double     Z;
  39.     double  cOverP = (double)compoundsPerYear/(double)paymentsPerYear;
  40.     double  pOverC = (double)paymentsPerYear/(double)compoundsPerYear;
  41.     double     i = (annualInterestPercent/100.0) / compoundsPerYear;
  42.     if (pOverC <= 1.0) {
  43.         Q = pow(1.0 + i, cOverP) - 1.0;
  44.         n = paymentsPerYear * years;
  45.         Z = (1.0 + Q);
  46.         }
  47.     else {
  48.         Q = i;
  49.         n = paymentsPerYear * years * cOverP;
  50.         Z = (pOverC + 1.0) * (Q / 2.0) + pOverC;        
  51.         }
  52.     double factor = Q / (pow(1.0 + Q, n) - 1);
  53.     return (payment * Z / factor);
  54. }
  55.  
  56. //-------------------------------------------------------------------
  57. double    
  58. AnnuityPresentValue(double payment,
  59.                     double annualInterestPercent, 
  60.                     short paymentsPerYear, 
  61.                     double years)
  62. {
  63.     // H-P 67 Standard Pac, p. 05-02
  64.     short    n = paymentsPerYear * years;
  65.     double     i = (annualInterestPercent/100.0) / paymentsPerYear;
  66.     double  factor = 1.0 - pow(1.0 + i, -n);
  67.     return payment * factor / i;    
  68. }
  69.  
  70. //-------------------------------------------------------------------
  71. double    
  72. AnnuityMonths(double annuity,
  73.                 double monthlyWithdrawal,
  74.                 double annualInterestPercent)
  75. {
  76.     // H-P 67 Standard Pac, p 05-02.
  77.     // annuity = Present Value
  78.     const double kMonthsPerYear = 12.0;
  79.     double     i = (annualInterestPercent/100.0) / kMonthsPerYear;
  80.     double  factor1 = log( 1.0 - (i * annuity/ monthlyWithdrawal) );
  81.     double  factor2 = log( 1.0 + i );
  82.     return  - factor1 / factor2;
  83. }
  84.  
  85. //-------------------------------------------------------------------
  86. double    
  87. Payment(double amountBorrowed,
  88.                     double annualInterestPercent, 
  89.                     double years)
  90. {
  91.     // H-P 67 Standard Pac, p 05-02.
  92.     // amountBorrowed = Present Value
  93.     const double kMonthsPerYear = 12.0;
  94.     short    n = kMonthsPerYear * years;
  95.     double     i = (annualInterestPercent/100.0) / kMonthsPerYear;
  96.     double  factor = 1 - pow(1.0 + i, -n);
  97.     return (i * amountBorrowed) / factor;
  98. }
  99.  
  100. //======================================================================
  101. // Loan functions
  102. double    
  103. LoanAmount(double monthlyPayment,
  104.             double annualInterestPercent, 
  105.             double years)
  106. {
  107.     // H-P 67 Standard Pac, p 05-02.
  108.     // amountBorrowed = Present Value
  109.     const double kMonthsPerYear = 12.0;
  110.     short    n = kMonthsPerYear * years;
  111.     double     i = (annualInterestPercent/100.0) / kMonthsPerYear;
  112.     double  factor = 1 - pow(1.0 + i, -n);
  113.     return (monthlyPayment / i) * factor;
  114. }
  115.  
  116. //-------------------------------------------------------------------
  117. double    
  118. LoanMonths(double amountBorrowed,
  119.                 double monthlyPayment,
  120.                 double annualInterestPercent)
  121. {
  122.     // H-P 67 Standard Pac, p 05-02.
  123.     // amountBorrowed = Present Value
  124.     const double kMonthsPerYear = 12.0;
  125.     double     i = (annualInterestPercent/100.0) / kMonthsPerYear;
  126.     double  factor1 = log( 1.0 - (i * amountBorrowed/ monthlyPayment) );
  127.     double  factor2 = log( 1.0 + i );
  128.     return  - factor1 / factor2;
  129. }
  130.  
  131. //-------------------------------------------------------------------
  132. double    
  133. InterestRate(double amountBorrowed,
  134.                 double monthlyPayment,
  135.                 double years)
  136. {
  137.     // H-P 67 Standard Pac, p 05-02.
  138.     return 0;
  139. }
  140.  
  141. //-------------------------------------------------------------------
  142. double    
  143. BalloonPayment(double amountBorrowed,
  144.                 double monthlyPayment,
  145.                 double annualInterestPercent,
  146.                 double years)
  147. {
  148.     // H-P 67 Standard Pac, p 05-06.
  149.     const double kMonthsPerYear = 12.0;
  150.     double     i = (annualInterestPercent/100.0) / kMonthsPerYear;
  151.     short    n = kMonthsPerYear * years;
  152.     double  factor1 = pow(1.0 + i, -n);
  153.     double    factor2 = (monthlyPayment / i) * ( 1.0 - factor1);
  154.     return  (amountBorrowed - factor2) / factor1;
  155. }
  156.