使用Java BigDecimal提高准确性

时间:2015-12-22 20:54:13

标签: java math

R = new BigDecimal(2.79E+00);
Dxm3d = new BigDecimal(3.99E-04);
Wmd = new BigDecimal(2.39E-03);
x = new BigDecimal(3.2);
t = new BigDecimal(365);

以下是公式

这些值J263是代表我的变量的excel选票。

  • J253值:2,39E-03是Wmd
  • J254值:3,99E-04是Dxm3d
  • J 255值:2,79E00是R
  • I259:365是t
  • J263值:3.2是x

enter image description here

enter image description here

BigDecimal segundoTermo = (R.multiply(x).subtract(Wmd.multiply(t)).divide(new BigDecimal(2d).multiply(new BigDecimal(Math.sqrt(Dxm3d.multiply(R).multiply(t).doubleValue()))), RoundingMode.HALF_DOWN));
System.out.println("value segundoTermo " + segundoTermo);

返回值

valor do segundo termo pfv:6.31838147917065306052600332590254032941338413886611227745342947009953030493273342105799365116070956364

预期值

6,321092458

1 个答案:

答案 0 :(得分:1)

我说:

  

我打赌你看到的差异是由于Math.sqrt()   需要一倍的电话。你可以看一下JScience和   this answer

但最终使用JScience的FloatingPoint从问题返回与BigDecimal相同的值:

FloatingPoint R = FloatingPoint.valueOf(2.79E+00);
FloatingPoint Dxm3d = FloatingPoint.valueOf(3.99E-04);
FloatingPoint Wmd = FloatingPoint.valueOf(2.39E-03);
FloatingPoint x = FloatingPoint.valueOf(3.2);
FloatingPoint t = FloatingPoint.valueOf(365);
FloatingPoint segundoTermo = (R.times(x).minus(Wmd.times(t)).divide(FloatingPoint.valueOf(2d).times(((Dxm3d.times(R).times(t)).sqrt()))));
System.out.println("value segundoTermo " + segundoTermo);

输出:

value segundoTermo 0.63188145784374107900E1

您确定预期值是否正确?