سؤال

I am writing a program in which I have to repeatedly multiply two BigDecimal.

After the 27th iteration, I get:

0.905225895893387921845435055445776361046057346344145563957027726

Any further calculations using this number (BigDecimal * 0.182365681285) results in trailing zeros. So the next iteration returns: 0.905225895893387921845435055445776361046057346344145563957027726000000000000

The iteration after that returns: 0.905225895893387921845435055445776361046057346344145563957027726000000000000000000000000

etc..


So I was wondering if this was due to some precision issue with BigDecimal.

Any help is appreciated

Edit: I was asked to post my code. I could copy paste the few pages I have, but this is a very accurate representation of what I have so far:

BigDecimal range = new BigDecimal(0.0012440624293146346);
for(int i = 0; i < 50 ; i++){
    low = low.multiply(range);
}
هل كانت مفيدة؟

المحلول

The multiply method creates a BigDecimal which has a scale equal to the scale of the first operand + the scale of the second operand, even if that leads to trailing zeros. To strip the trailing zeros, use... stripTrailingZeros().

نصائح أخرى

I have found a situation while simulates trailing zeros.

BigDecimal bd = BigDecimal.valueOf(0.5);
BigDecimal a = BigDecimal.valueOf(1 << 20).divide(BigDecimal.valueOf(1000000));
for (int i = 0; i < 20; i++) {
    System.out.println(a);
    a = a.multiply(bd);
}

Note: BigDecimal doesn't assume you want to strip trailing zeros by default

1.048576
0.5242880
0.26214400
0.131072000
0.0655360000
0.03276800000
0.016384000000
0.0081920000000
0.00409600000000
0.002048000000000
0.0010240000000000
0.00051200000000000
0.000256000000000000
0.0001280000000000000
0.00006400000000000000
0.000032000000000000000
0.0000160000000000000000
0.00000800000000000000000
0.000004000000000000000000
0.0000020000000000000000000

There is no reason you should be getting zero's which shouldn't be there unless you are multiplying double values converted to BigDecimal each time.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top