Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# BigInteger math

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 1970
• Number of slices to send:
Optional 'thank-you' note:
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.

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
[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.

author
Posts: 4323
39
• Number of slices to send:
Optional 'thank-you' note:
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.

 It's exactly the same and completely different as this tiny ad: free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter