Skip to content

Commit

Permalink
Added apr_interest function to financial (TheAlgorithms#6025)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jun 19, 2023
1 parent b0f8710 commit ea6c605
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions financial/interest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def simple_interest(
principal: float, daily_interest_rate: float, days_between_payments: int
principal: float, daily_interest_rate: float, days_between_payments: float
) -> float:
"""
>>> simple_interest(18000.0, 0.06, 3)
Expand Down Expand Up @@ -42,7 +42,7 @@ def simple_interest(
def compound_interest(
principal: float,
nominal_annual_interest_rate_percentage: float,
number_of_compounding_periods: int,
number_of_compounding_periods: float,
) -> float:
"""
>>> compound_interest(10000.0, 0.05, 3)
Expand Down Expand Up @@ -77,6 +77,43 @@ def compound_interest(
)


def apr_interest(
principal: float,
nominal_annual_percentage_rate: float,
number_of_years: float,
) -> float:
"""
>>> apr_interest(10000.0, 0.05, 3)
1618.223072263547
>>> apr_interest(10000.0, 0.05, 1)
512.6749646744732
>>> apr_interest(0.5, 0.05, 3)
0.08091115361317736
>>> apr_interest(10000.0, 0.06, -4)
Traceback (most recent call last):
...
ValueError: number_of_years must be > 0
>>> apr_interest(10000.0, -3.5, 3.0)
Traceback (most recent call last):
...
ValueError: nominal_annual_percentage_rate must be >= 0
>>> apr_interest(-5500.0, 0.01, 5)
Traceback (most recent call last):
...
ValueError: principal must be > 0
"""
if number_of_years <= 0:
raise ValueError("number_of_years must be > 0")
if nominal_annual_percentage_rate < 0:
raise ValueError("nominal_annual_percentage_rate must be >= 0")
if principal <= 0:
raise ValueError("principal must be > 0")

return compound_interest(
principal, nominal_annual_percentage_rate / 365, number_of_years * 365
)


if __name__ == "__main__":
import doctest

Expand Down

0 comments on commit ea6c605

Please sign in to comment.