forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterest.py
25 lines (21 loc) · 904 Bytes
/
interest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Compound interest function with ``decimal.Decimal``
"""
def compound_interest(principal, rate, periods):
return principal * ((1 + rate) ** periods - 1)
def test(verbose=False):
from decimal import Decimal, getcontext
getcontext().prec = 8
fixture = [(1000, Decimal('0.05'), 1, Decimal('50')),
(1000, Decimal('0.10'), 5, Decimal('610.51')),
(1000, Decimal('0.10'), 15, Decimal('3177.2482')),
(1000, Decimal('0.06'), 5, Decimal('338.2256')),
]
for principal, rate, periods, future_value in fixture:
computed = compound_interest(principal, rate, periods)
if verbose:
print('{!r}, {!r}, {!r} -> {!r}'.format(
principal, rate, periods, computed))
assert future_value == computed, '{!r} != {!r}'.format(future_value, computed)
if __name__ == '__main__':
test(True)