Я получаю эту ошибку, когда выполняю annuity_rate(5, 100, 510)
или пытаюсь использовать отрицательные значения. Как я могу это исправить?
Хорошо работает с большим числом, но почему-то не работает для отрицательных и маленьких чисел.
def pv_annuity(r, n, pmt):
""" Return the present value of an annuity of pmt to be received
each period for n periods"""
pv = pmt * (1 - (1 + r) ** (-n)) / r
return pv
def annuity_rate(n, pmt, pv):
""" return the rate of interest required to amortize the pv in n periods
with equal periodic payments of pmt"""
rate_low, rate_high = 0, 1
while True:
rate = (rate_high + rate_low) / 2
#print('trying rate', rate)
test_pv = pv_annuity(rate, n, pmt)
#print(test_pv)
if abs(pv - test_pv) <= 0.01:
break
if test_pv > pv:
rate_low = (rate_high + rate_low) / 2
if test_pv < pv:
rate_high = (rate_high + rate_low) / 2
return rate