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 BigInteger math Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigInteger math" Watch "BigInteger math" New topic
Author

BigInteger math

Eric Crockett
Greenhorn

Joined: Jan 24, 2005
Posts: 25
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.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


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

Joined: Jan 30, 2000
Posts: 18671
[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
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigInteger math