[英]P/Y C/Y - Calculate future value (FV) from payments (PMT) with compounding period per year (C/Y)
I am doing a TVM project and not quite understand how the compound interest translates into code.我正在做一个 TVM 项目,但不太了解复利如何转化为代码。 I am using BA II Plus financial calculator as a reference.我使用 BA II Plus 财务计算器作为参考。
Example: Find the future value of $100 payments made at the beginning of every month for 10 years if interest is 5% compounded quarterly.示例:如果每季度复利 5%,则计算 10 年内每月月初支付的 100 美元的未来价值。
In the financial calculator:在财务计算器中:
N: 120 (10y x 12m) N: 120 (10y x 12m)
I/Y: 5% (annually interest rate) I/Y:5%(年利率)
P/Y: 12 (12 times per year) P/Y:12(每年12次)
C/Y: 4 (4 times per year) C/Y:4(每年4次)
PV: 0 PV:0
PMT: 100时间:100
BGN: TRUE BGN:正确
FV: [CPT] [FV] => -15575.41334 FV:[CPT] [FV] => -15575.41334
Here is the future value method.这是未来值方法。
static public double fv(double r, int n, double pmt, double pv, int bgn) {
return -(pv * Math.pow(1 + r, n) + pmt * (1 + r * bgn) * (Math.pow(1 + r, n) - 1) / r);
}
Call the method using numbers from the example使用示例中的数字调用该方法
// rate n pmt pv bgn
double fv = fv(0.05/12, 120, 100, 0, 1); // -15592.928894335751 which is wrong
It seems you are getting numerical errors in the computation.看来您在计算中遇到了数值错误。
Floating-point computations are inherently subject to rounding errors, and these can add up and become very large.浮点计算本质上会受到舍入误差的影响,并且这些误差可以累加并变得非常大。
Eg general background (long read):例如一般背景(长读):
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
You can try using a high-precision computation library.您可以尝试使用高精度计算库。
Eg some good options are mentioned in this answer:例如,此答案中提到了一些不错的选择:
https://stackoverflow.com/a/284588/9549171 https://stackoverflow.com/a/284588/9549171
edit: you could try to analyse the expression you are using with https://herbie.uwplse.org/ (but the web version times out on your expression, you'll have to download their software)编辑:您可以尝试使用https://herbie.uwplse.org/分析您正在使用的表达式(但网络版本在您的表达式上超时,您必须下载他们的软件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.