【发布时间】:2019-05-03 04:08:36
【问题描述】:
我正在尝试重新创建此app。但是,对于存款频率与复利周期不匹配的情况,我的函数输出与链接应用程序的投资总价值输出不匹配。
这是我的功能...
def compound_interest(principal, pmt, rate, frequency, period, time):
contribution_frequencies = {'weekly' : 52, 'biweekly' : 26, 'monthly' : 12, 'quarterly' : 4, 'semiannually' : 2, 'yearly' : 1}
compounding_periods = {'monthly' : 12, 'quarterly' : 4, 'semiannually' : 2, 'yearly' : 1}
frequency = contribution_frequencies[frequency]
period = compounding_periods[period]
rate = rate / 100
principal_interest = principal * (1 + (rate / period)) ** (period * time)
fv = (pmt * frequency) / period * ((1 + (rate / period)) ** (period * time) - 1) / (rate / period)
total = principal_interest + fv
return round(total, 2)
这是我的测试,存款频率与复利周期相同...
print(compound_interest(5000, 100, 5, 'monthly', 'monthly', 15))
print(compound_interest(5000, 100, 5, 'yearly', 'yearly', 15))
print(compound_interest(5000, 100, 5, 'quarterly', 'quarterly', 15))
print(compound_interest(5000, 100, 5, 'semiannually', 'semiannually', 15))
下面从我的函数返回的实际值与我从链接应用的输出中获得的实际值相同...
37297.41
12552.5
19393.36
14878.11
对于上述以外的情况,测试的实际值与链接应用的实际值不同。比如……
print(compound_interest(5000, 100, 5, 'weekly', 'monthly', 15))
返回...
126393.73
而链接的app 返回...
126579.19
请记住,我的公式计算的是在复利期结束时进行的额外存款(或者它是 says),这似乎与链接应用程序的相同。
对于存款频率和复利周期的所有组合,我将如何重写我的函数,使其返回的实际值与链接应用程序的实际值相同?
谢谢!
【问题讨论】: