Trying to finish up a program and got stuck (again ) I've been using BigInteger to multiply large positive exponents, now I'm trying to use BigDecimal to multiply large negative exponents. So far no luck. I was told that dividing is the same as multiplying negative exponents. Works fine with double on small negative exponents, but not on large. BigDecimal stops at zero? Why is this? Is there a workaround? Was I given bad info, am I even in the right ballpark? Here's some code to demonstrate the problem: import java.math.*; public class Test { public static void main(String[] args) { //say the exponent is -10 //using BigDecimal BigDecimal bd1 = BigDecimal.valueOf(20); BigDecimal bd2 = BigDecimal.valueOf(20); for(int j = -2; j > -10; j--) { bd1= bd1.divide(bd2,10,BigDecimal.ROUND_DOWN); System.out.println("Big Decimal = " +bd1); } //using double double d1 = 20; double d2 = 20; for(int k = -2; k > -10; k--) { d1 = d1 / d2; System.out.println("double = " +d1); } }//end main } What's going on? Anybody studied this topic before? Thanks, Tom [ March 30, 2004: Message edited by: Tom Thumb ]

public static void main(String[] args) { //say the exponent is -10 //using BigDecimal BigDecimal bd1 = BigDecimal.valueOf(20); BigDecimal bd2 = BigDecimal.valueOf(20); for(int j = -2; j > -10; j--) { bd1= bd1.divide(bd2,10,BigDecimal.ROUND_DOWN); System.out.println("Big Decimal = " +bd1); } Looks to me like you might just have a logic error, or an incorrect expectation. Let's play CPU/ALU... bd1 = 20 bd2 = 20 we enter the loop we divide 20/20 = 1 next iteration we divide 1 by 20 = .05 next iteration we divide .05 by 20 = .0025 See the pattern? You'll never go negative you are on a infinte number curve approaching, but never going beyond 0. You will never go negative given this scenario. I think you actually had something else in mind using the negative numbers in your loop, but you didn't employ them in your algorithm, so all it does it cause the loop to execute 9 times.

Hi Byron, I understand what you're saying, I must be blind. If you run the test code everything's the same til the third iteration. Then double follows with 1.24E-4 while BigDecimal follows with 0.000125000 I just missed it because of the formatting. I'll see if I can come up with the formatting code, if someone knows it, it would be appreciated. I need to make BigDecimal output like double, e.g., with an exponent. BTW, if anyone knows of a way to simply multiply with a large negative exponent I'd love to see it. Thanks to all, Tom [ March 30, 2004: Message edited by: Tom Thumb ]

Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313

posted

0

In order to get you BigDecimal formatted in "scientific notation" like a double (...that's the default representation), just call the doubleValue() method instead of only referring to the variable inself. This will return a double whose default display format is scientific notation.

bd.doubleValue();

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com