我正在尝试用 Java 创建抵押计算器,但我在数学问题上遇到了麻烦。 本质上,我需要加注双倍但不能。我也尝试过 (Math.pow) 但它返回无限,因为我的指数是负整数。有人有推荐吗
int n = 12;
int t = 30;
double r = 0.05;
int nt = n * t; // = 360
nt = -nt; // = -360
double principal = 1.0 - (1.0 + r / n); // = -0.004166666666666652
double result = Math.pow(principal, nt); // = Infinity
System.out.println(result);
有没有办法将
Principal
提高到 nt
的次方?
垃圾进,垃圾出。您的代码描述了通向无限的过程。换句话说,问题的答案:
如何为我的 Java 抵押代码执行“加薪”
就是:你就是这样做的。
因此,存在一个根本问题:您未能编写复合计算程序。
您真正要找的是:
(1 + r/n)^nt.
您在某处添加了
1.0 - ...
。不要那样做。由于某种原因,您还向 nt
添加了 - 。也不要这样做:
int n = 12;
int t = 30;
double r = 0.05;
int nt = n * t; // = 360
// nt = -nt; // = -360 DO NOT DO THIS
// double principal = 1.0 - (1.0 + r / n); // = -0.004166666666666652
double principal = 1.0 + r / n;
double result = Math.pow(principal, nt);
System.out.println(result);
这将打印 4.467744314006109。
哪个是正确的:如果您投资 X 欧元购买每年支付 5% 利息、每月支付的投资,并且您将其投资 12 年,那么您最终获得的金额将略低于投资金额的 4.5 倍。