File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes multiplying large Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "multiplying large "negative exponents"?" Watch "multiplying large "negative exponents"?" New topic
Author

multiplying large "negative exponents"?

Tom Clement
Greenhorn

Joined: Mar 21, 2004
Posts: 26
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 ]
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
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.


Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Tom Clement
Greenhorn

Joined: Mar 21, 2004
Posts: 26
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
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: multiplying large "negative exponents"?