这里浮点精度差异的原因可能是什么?
def "emi test"(){
given:
def P = 6000000.00
def n = 20 * 12
def r = (8.35/12)/100
double emi_g = (P * r * (1+r).power(n)) / ((1+r).power(n) - 1)
double emi_j= (P * r * Math.pow(1+r,n)) / (Math.pow(1+r,n)-1);
expect:
emi_g == emi_j
}
结果:
emi_g == emi_j
| | |
| | 51501.177737160346
| false
51501.177737160666
最佳答案
结果的数据类型解释了差异。
在 shell 中运行时,结果类型为:
groovy:000> emi_g.getClass()
===> class java.math.BigDecimal
groovy:000> emi_j.getClass()
===> class java.lang.Double
Groovy 的实现返回一个 BigDecimal
,它比 Double
具有更好的精度
关于java - Groovy 与 Java - 浮点精度的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49913438/