Re: Smarter than BigDecimal Class



Luc The Perverse wrote:
So you have to be smarter than the class that you're using. Apparently I don't qualify.

So I have 3 BigIntegers and I need to divide them with high precision and then convert them to doubles (in an array) and they need to be accurate to within 1E-9.

My code was supposed to be very simple, but it kept rounding to whole integers, even after I called the function setScale(40, BigDecimal.ROUND_FLOOR) (Note: I tried EVERY variation of rounding parameters including unnecessary, which as expected, threw an exception.) Eventually I got the following convoluted mess to work. (mass, X and Y are all BigInteger class instances with values from preceding code)

if(mass.equals(BigInteger.ZERO))
return new double[0]; //special case
BigDecimal massTimesTwo = new BigDecimal(mass.multiply(BigInteger.valueOf(2))).add(BigDecimal.valueOf(1E-30));
massTimesTwo.setScale(40, BigDecimal.ROUND_FLOOR);
BigDecimal bdX = new BigDecimal(X).add(BigDecimal.valueOf(1E-30));
bdX.setScale(40, BigDecimal.ROUND_FLOOR);

This seems suspicious to me. You call setScale, but do nothing with its
result.

I tried the following:

import java.math.BigDecimal;
public class TestBigDecimal {
public static void main(String[] args) {
BigDecimal x = new BigDecimal(20).setScale(20);
BigDecimal d = new BigDecimal(3);
System.out.println(x.divide(d, BigDecimal.ROUND_UP));
}
}

and got output:

6.66666666666666666667

Patricia
.