MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / compound_interest

Function compound_interest

financial/interest.py:42–77  ·  view source on GitHub ↗

>>> compound_interest(10000.0, 0.05, 3) 1576.2500000000014 >>> compound_interest(10000.0, 0.05, 1) 500.00000000000045 >>> compound_interest(0.5, 0.05, 3) 0.07881250000000006 >>> compound_interest(10000.0, 0.06, -4) Traceback (most recent call last): ... V

(
    principal: float,
    nominal_annual_interest_rate_percentage: float,
    number_of_compounding_periods: float,
)

Source from the content-addressed store, hash-verified

40
41
42def compound_interest(
43 principal: float,
44 nominal_annual_interest_rate_percentage: float,
45 number_of_compounding_periods: float,
46) -> float:
47 """
48 >>> compound_interest(10000.0, 0.05, 3)
49 1576.2500000000014
50 >>> compound_interest(10000.0, 0.05, 1)
51 500.00000000000045
52 >>> compound_interest(0.5, 0.05, 3)
53 0.07881250000000006
54 >>> compound_interest(10000.0, 0.06, -4)
55 Traceback (most recent call last):
56 ...
57 ValueError: number_of_compounding_periods must be > 0
58 >>> compound_interest(10000.0, -3.5, 3.0)
59 Traceback (most recent call last):
60 ...
61 ValueError: nominal_annual_interest_rate_percentage must be >= 0
62 >>> compound_interest(-5500.0, 0.01, 5)
63 Traceback (most recent call last):
64 ...
65 ValueError: principal must be > 0
66 """
67 if number_of_compounding_periods <= 0:
68 raise ValueError("number_of_compounding_periods must be > 0")
69 if nominal_annual_interest_rate_percentage < 0:
70 raise ValueError("nominal_annual_interest_rate_percentage must be >= 0")
71 if principal <= 0:
72 raise ValueError("principal must be > 0")
73
74 return principal * (
75 (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods
76 - 1
77 )
78
79
80def apr_interest(

Callers 1

apr_interestFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected