Stu*_*ent 0 java math floating-point android ieee
我正在研究抵押贷款计算公式,我从Math.pow()得到了不同的结果,我不知道为什么.
这是测试设置:
double interestRatePercent = 7;
double monthlyInterestRate = (interestRatePercent / 100) / MONTHS_PER_YEAR;
int numberOfPayments = loanTermInYears * MONTHS_PER_YEAR;
Log.i(TAG, String.format("monthlyInterestRate: %f", monthlyInterestRate));
Log.i(TAG, String.format("numberOfPayments: %d", numberOfPayments));
Log.i(TAG, " ");
Log.i(TAG, "Hardcoded result:");
double hardcodedResult = Math.pow(1.0 + 0.005833, numberOfPayments);
Log.i(TAG, String.format("(1 + 0.005833)^360 = %f", hardcodedResult));
Log.i(TAG, " ");
Log.i(TAG, "Parameterized result:");
double paramResult = Math.pow(1.0 + monthlyInterestRate, numberOfPayments);
Log.i(TAG, String.format("(1 + %f)^%d = %f", monthlyInterestRate, numberOfPayments, paramResult));
Log.i(TAG, " ");
Log.i(TAG, "BigDecimal result:");
BigDecimal bigResult = BigDecimal.valueOf(1.0 + monthlyInterestRate);
bigResult = bigResult.pow(numberOfPayments);
Log.i(TAG, String.format("(1 + %f)^%d = %f", monthlyInterestRate, numberOfPayments, bigResult));
Log.i(TAG, " ");
Log.i(TAG, " ");
Run Code Online (Sandbox Code Playgroud)
这是测试结果:
monthlyInterestRate: 0.005833
numberOfPayments: 360
Hardcoded result:
(1 + 0.005833)^360 = 8.115529
Parameterized result:
(1 + 0.005833)^360 = 8.116497
BigDecimal result:
(1 + 0.005833)^360 = 8.116497
Run Code Online (Sandbox Code Playgroud)
只有硬编码的结果是正确的.Math.pow和BigDecimal.pow结果都很糟糕.
有任何想法吗?
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |