Eric Crockett

Greenhorn

Posts: 25

posted 11 years ago

I know how to take a primitive value to a decimal exponent, but how do you do this to a BigInteger too large to be cast to a primitive type? Is there a way to find square or cube roots of a BigInteger?

The only other thing I need, but can't find is how to take find the log of a bigInteger value.

If anyone knows if any of these things are possible OR NOT, please reply! Thanks in advance for any help.

The only other thing I need, but can't find is how to take find the log of a bigInteger value.

If anyone knows if any of these things are possible OR NOT, please reply! Thanks in advance for any help.

Peter Chase

Ranch Hand

Posts: 1970

posted 11 years ago

BigDecimal and BigInteger do not provide any methods to do roots or "to the power of" operations.

There are no exact root or "to the power of" operations available on primitives, either. There are no root or "to the power of" operations at all on integers. They do exist for "float" and "double", but are approximate. Of course, they couldn't be anything else, because "float" and "double" are always approximate. Even if you set a "double" variable to 9.0, it may not actually be set to exactly nine, and its square-root will not come out as exactly three.

Computers generally implement powers via an approximation (polynomial series, or something like that). Therefore, the "to the power of" operation is perhaps not appropriate for BigDecimal or BigInteger, which only do exact operations.

You should convert your BigDecimal to a "double", use the appropriate Math methods on it, and convert it back, remembering that the result is approximate.

There are no exact root or "to the power of" operations available on primitives, either. There are no root or "to the power of" operations at all on integers. They do exist for "float" and "double", but are approximate. Of course, they couldn't be anything else, because "float" and "double" are always approximate. Even if you set a "double" variable to 9.0, it may not actually be set to exactly nine, and its square-root will not come out as exactly three.

Computers generally implement powers via an approximation (polynomial series, or something like that). Therefore, the "to the power of" operation is perhaps not appropriate for BigDecimal or BigInteger, which only do exact operations.

You should convert your BigDecimal to a "double", use the appropriate Math methods on it, and convert it back, remembering that the result is approximate.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 11 years ago

Sure they do. The methods are confusingly called pow() however, so they're easy to overlook. You can only raise to an integer power, no decimals - so you can't do roots. But pow() is exact; no approximations. I agree with Peter's reasoning about why fractional powers are not supported here. I believe that implementing these would've either been incredibly slow, or much, much less accurate than we get with other BigDecimal operations. People might get a number that looks like it has 50 digits, but only the first ten are accurate. For fractional powers, best to convert both arguments to double and live with the approximation.

It's not quite true that BigInteger and BigDecimal "only do exact operations". If you divide 1.0 by 3.0 using BigDecimal's divide() methods, the answer will be rounded at some point; it has to be. But at least you get to specify how accurate you want it to be, based on how much time and memory you're willing to spend on it.

**[Peter]: BigDecimal and BigInteger do not provide any methods to do roots or "to the power of" operations.**

Sure they do. The methods are confusingly called pow() however, so they're easy to overlook. You can only raise to an integer power, no decimals - so you can't do roots. But pow() is exact; no approximations. I agree with Peter's reasoning about why fractional powers are not supported here. I believe that implementing these would've either been incredibly slow, or much, much less accurate than we get with other BigDecimal operations. People might get a number that looks like it has 50 digits, but only the first ten are accurate. For fractional powers, best to convert both arguments to double and live with the approximation.

It's not quite true that BigInteger and BigDecimal "only do exact operations". If you divide 1.0 by 3.0 using BigDecimal's divide() methods, the answer will be rounded at some point; it has to be. But at least you get to specify how accurate you want it to be, based on how much time and memory you're willing to spend on it.

"I'm not back." - Bill Harding, *Twister*

posted 11 years ago

I don't think we answered his original question, how do you take a BigInteger to a decimal exponent that is too big to be cast as a primitive?

The answer is that you can't natively do this in java, but there is nothing stopping you from writing your own method that would do this:

public static BigInteger pow(BigInteger x, double exponent)

As suggested, it may perform poorly but if you need to do it (for example if you are coding a math application) then the performance cost can not be avoided (although it can usually be improved depending on how smart your algorithm is).

I would search the internet for one of two things, a copy of this function that someone has all ready written, or a crash course on how to compute decimal exponents of numbers using series and/or bit operations.

The answer is that you can't natively do this in java, but there is nothing stopping you from writing your own method that would do this:

public static BigInteger pow(BigInteger x, double exponent)

As suggested, it may perform poorly but if you need to do it (for example if you are coding a math application) then the performance cost can not be avoided (although it can usually be improved depending on how smart your algorithm is).

I would search the internet for one of two things, a copy of this function that someone has all ready written, or a crash course on how to compute decimal exponents of numbers using series and/or bit operations.

[OCA 8 Book] [OCP 8 Book] [Blog] * SCJP (1.4, 1.6) * OCAJP 8 * OCPJP 8