4
I have two formulas implemented, one with the Bigdecimal type and the other with the primitive double type, but the results differ and I am not able to find the solution. Below are the formulas:
BigDecimal x = new BigDecimal(3.2d); //TODO: entradas do usuário
BigDecimal t = new BigDecimal(365d);
/**
* Convertendo valor de v para m/d
*/
//double Vmd = v * Math.pow(10d, -2d);
BigDecimal Vmd = new BigDecimal(v * Math.pow(10d, -2d));
/**
* convertendo valor de W para m/d
*/
//double Wmd = W / 100d;
BigDecimal Wmd = new BigDecimal(W / 100d);
BigDecimal div = new BigDecimal(1d/2d);
BigDecimal teste = div.multiply(new BigDecimal(Math.exp(primeiroTermo.doubleValue()))).
multiply(new BigDecimal( Erf.erfc(seguntoTermo.doubleValue())).add(div)
.multiply(new BigDecimal(Math.exp(terceiroTermo.doubleValue())))
.multiply(new BigDecimal(Erf.erfc(quartoTermo.doubleValue()))));
System.out.println("Valor total de Bxt em BigDecimal: " + nb.format(teste));
Bxt = (1d/2d) * (Math.exp(primeiroTermo.doubleValue())) * Erf.erfc(seguntoTermo.doubleValue()) + (1d/2d) * (Math.exp(terceiroTermo.doubleValue())) * Erf.erfc(quartoTermo.doubleValue());
System.out.println("Valor de BXT: em Double " + nb.format(Bxt));
Final Value:
Valor total de Bxt em BigDecimal: 1,63E6
Valor de BXT: em Double 6,41E-20
Valor esperado : 6,19E-20
Term values: (input values for formulas)
valor do primeiro termo : -1,75E0
valor do segundo termo :6.31838147917065306052600332590254032941338413886611227745342947009953030493273342105799365116070956364
valor do terceiro termo :1,74E1
valor do quartoTermo termo :7.68858730163961471709076409364321640056053538848914223720418242638997018233141713029583833874949308593
The closest result to the correct is the primitive double
What is the
div
?– Math
There is another error: the expression with double has a factor of 1/2 and the Bigdecimal does not.
– Alexandre Cartaxo
Alexandre a váriavel div é o que faz esse 1/2
– Jose Vieira Neto
@Math is the 1/2 you have in the other formula
– Jose Vieira Neto
Never use any of that, but I believe the error lies in the way you are incorrectly applying the add method. Try separating the test variable into two terms and adding them later to the part.
– Alexandre Cartaxo
See also this: http://stackoverflow.com/questions/12944559/how-to-multiply-a-bigdecimal-by-an-integer-in-java
– Alexandre Cartaxo
This question you pointed out I already use. :(
– Jose Vieira Neto
Are you sure? Because it looks like you’re testing=(1/2thermo_atermo_b) + 1/2) * termo_c * termo_4 and not what you want...
– Alexandre Cartaxo