Skip to content

Commit 7464e70

Browse files
smturro2cclausspre-commit-ci[bot]
authored andcommitted
Added apr_interest function to financial (TheAlgorithms#6025)
* Added apr_interest function to financial * Update interest.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update financial/interest.py * float --------- Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a680a0e commit 7464e70

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Diff for: financial/interest.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
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
88
) -> float:
99
"""
1010
>>> simple_interest(18000.0, 0.06, 3)
@@ -42,7 +42,7 @@ def simple_interest(
4242
def compound_interest(
4343
principal: float,
4444
nominal_annual_interest_rate_percentage: float,
45-
number_of_compounding_periods: int,
45+
number_of_compounding_periods: float,
4646
) -> float:
4747
"""
4848
>>> compound_interest(10000.0, 0.05, 3)
@@ -77,6 +77,43 @@ def compound_interest(
7777
)
7878

7979

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+
80117
if __name__ == "__main__":
81118
import doctest
82119

0 commit comments

Comments
 (0)