|
4 | 4 |
|
5 | 5 |
|
6 | 6 | def simple_interest(
|
7 |
| - principal: float, daily_interest_rate: float, days_between_payments: int |
| 7 | + principal: float, daily_interest_rate: float, days_between_payments: float |
8 | 8 | ) -> float:
|
9 | 9 | """
|
10 | 10 | >>> simple_interest(18000.0, 0.06, 3)
|
@@ -42,7 +42,7 @@ def simple_interest(
|
42 | 42 | def compound_interest(
|
43 | 43 | principal: float,
|
44 | 44 | nominal_annual_interest_rate_percentage: float,
|
45 |
| - number_of_compounding_periods: int, |
| 45 | + number_of_compounding_periods: float, |
46 | 46 | ) -> float:
|
47 | 47 | """
|
48 | 48 | >>> compound_interest(10000.0, 0.05, 3)
|
@@ -77,6 +77,43 @@ def compound_interest(
|
77 | 77 | )
|
78 | 78 |
|
79 | 79 |
|
| 80 | +def apr_interest( |
| 81 | + principal: float, |
| 82 | + nominal_annual_percentage_rate: float, |
| 83 | + number_of_years: float, |
| 84 | +) -> float: |
| 85 | + """ |
| 86 | + >>> apr_interest(10000.0, 0.05, 3) |
| 87 | + 1618.223072263547 |
| 88 | + >>> apr_interest(10000.0, 0.05, 1) |
| 89 | + 512.6749646744732 |
| 90 | + >>> apr_interest(0.5, 0.05, 3) |
| 91 | + 0.08091115361317736 |
| 92 | + >>> apr_interest(10000.0, 0.06, -4) |
| 93 | + Traceback (most recent call last): |
| 94 | + ... |
| 95 | + ValueError: number_of_years must be > 0 |
| 96 | + >>> apr_interest(10000.0, -3.5, 3.0) |
| 97 | + Traceback (most recent call last): |
| 98 | + ... |
| 99 | + ValueError: nominal_annual_percentage_rate must be >= 0 |
| 100 | + >>> apr_interest(-5500.0, 0.01, 5) |
| 101 | + Traceback (most recent call last): |
| 102 | + ... |
| 103 | + ValueError: principal must be > 0 |
| 104 | + """ |
| 105 | + if number_of_years <= 0: |
| 106 | + raise ValueError("number_of_years must be > 0") |
| 107 | + if nominal_annual_percentage_rate < 0: |
| 108 | + raise ValueError("nominal_annual_percentage_rate must be >= 0") |
| 109 | + if principal <= 0: |
| 110 | + raise ValueError("principal must be > 0") |
| 111 | + |
| 112 | + return compound_interest( |
| 113 | + principal, nominal_annual_percentage_rate / 365, number_of_years * 365 |
| 114 | + ) |
| 115 | + |
| 116 | + |
80 | 117 | if __name__ == "__main__":
|
81 | 118 | import doctest
|
82 | 119 |
|
|
0 commit comments