Increase Accuracy with BigDecimal Java

4

Tickets

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);

Follow the formula below

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("valor do segundo termo pfv:" + segundoTermo);

Return Value

valor do segundo termo pfv:6.31838147917065306052600332590254032941338413886611227745342947009953030493273342105799365116070956364

Expected Value

6,321092458

These J263 values are the execel banknotes that represent my variants.

  • J253 value: 2.39E-03 represents Wmd
  • J254 value: 3.99E-04 represents Dxm3d
  • J255 value: 2.79E + 00 represents the R
  • I259 value: 365 represents t
  • J263 value: 3.2 represents x

    
asked by anonymous 22.12.2015 / 19:43

3 answers

2

Very succinctly:

Mathcad (or other programs) uses a different number of significant digits for both calculations and error function values. The values for the function used by Erf.java are here and use an implementation in accordance with this .

    
22.12.2015 / 19:48
2

My suspicion is that the input values are approximate and therefore causing the deviation in the final result.

For example, the value 2.79E+00 seems to have been printed in scientific notation with two decimal places because this was the default of some software. In general, when a floating point number has only two decimal places, scientific notation is not adopted.

To illustrate what I am saying, add a few more decimal places to the value of R and get a closer value. See the example:

BigDecimal R = new BigDecimal("2.791654").setScale(20);
BigDecimal x = new BigDecimal("3.2").setScale(20);
BigDecimal w = new BigDecimal("0.00239").setScale(20);
BigDecimal t = new BigDecimal("365").setScale(20);

//R.x - w.t
BigDecimal dividendo = R.multiply(x).subtract(w.multiply(t));

BigDecimal D = new BigDecimal("0.000399").setScale(20);
BigDecimal dois = new BigDecimal("2").setScale(20);

//2 . (D.R.t) ^ 1/2
BigDecimal divisor = new BigDecimal(Math.sqrt(D.multiply(R).multiply(t).doubleValue())).multiply(dois);

BigDecimal segundoTermo = dividendo.divide(divisor, RoundingMode.HALF_DOWN);
System.out.println("valor do segundo termo pfv:" + segundoTermo);

I've made some changes to make the example clearer, as well as adding a greater number of decimal places, but all of this has little effect on the final result, other than the precision of the input numbers.

    
23.12.2015 / 02:21
0

Having the value, a setScale with Round Half Up, should generate the result you want.

BigDecimal valor = 123;
BigDecimal resultado = new BigDecimal(String.valueOf(valor)).setScale(9, BigDecimal.ROUND_HALF_UP);
    
22.12.2015 / 19:56