# multiplying large "negative exponents"?

Tom Clement

Greenhorn

Posts: 26

posted 12 years ago

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 ]

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

Posts: 313

posted 12 years ago

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.

//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

Posts: 26

posted 12 years ago

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 ]

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

Posts: 313

posted 12 years ago
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>

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();

bd.doubleValue();