L
Luc The Perverse
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);
BigDecimal bdY = new BigDecimal(Y).add(BigDecimal.valueOf(1E-30));
bdY.setScale(40, BigDecimal.ROUND_FLOOR);
double[] ret =
{
bdX.divide(massTimesTwo, BigDecimal.ROUND_UP).doubleValue(),
bdY.divide(massTimesTwo, BigDecimal.ROUND_UP ).doubleValue()
};
return ret;
I find it hard to believe that this mess is necessary Help?
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);
BigDecimal bdY = new BigDecimal(Y).add(BigDecimal.valueOf(1E-30));
bdY.setScale(40, BigDecimal.ROUND_FLOOR);
double[] ret =
{
bdX.divide(massTimesTwo, BigDecimal.ROUND_UP).doubleValue(),
bdY.divide(massTimesTwo, BigDecimal.ROUND_UP ).doubleValue()
};
return ret;
I find it hard to believe that this mess is necessary Help?