0

我试图在普通年金公式的现值中复制 BA II Plus 精度来查找项 (N)。具体来说,负利率(我不能使用自然对数)。我的代码在下面,但它既慢又不准确。有没有更好的方法可以做到这一点?

# code to find term (n) in present value of annuity formula, where the 
# interest rate is negative, i.e. -55.31257% in below example, as the
# general way to solve, i.e natural logarithms don't work with negative 
# interest rates.

pv = 1559606.4
pp = 100
i = -.5531257
# below is the desired answer
# n = 11.251357

# where interest rate is negative
for num in range(1,100000000):
   expn = num/1000000
   temp3 =  pv - pp*(1 - (1+i)**-expn)/i

   #set level of precision, i.e. accurate to within
   if temp3 <= 0.00000001:
       n = num/1000000
       print("The Number of periods is " + str(n))
       n = float(n)
       break   
4

1 回答 1

2

我会使用numpy.nper(),例如:

代码:

import numpy as np 
np.nper(i, -pp, pv)

测试代码:

pv = 1559606.4
pp = 100
i = -.5531257
# below is the desired answer
# n = 11.251357

import numpy as np

print(np.nper(i, pp, -pv))

结果:

11.2513570023
于 2019-01-07T01:25:18.090 回答