1

I'm a .NET developer, but apparently no library exists to do this in .NET. So, is there any BigInteger implementation in Java that allows raising a BigInteger to a double exponent:

BigInteger result = BigInteger.pow( base, dblExponent);
// Base is a BigInteger, dblExponent is a double.
Jonathan
  • 93
  • 1
  • 2
  • 9
  • 2
    The mathematical result would usually not be an integer, so providing such a function would need to make a dubious decision about how to interpret it. What are you actually trying to do? – Daniel Fischer May 25 '13 at 15:48
  • @DanielFischer That is not a problem, in my case I would always generate something that has a fractional part .00000 etc. So i can then convert to integer without losing data. But my question was whether such a function exists. – Jonathan May 25 '13 at 16:02
  • So raising your huge number to the nearest integer isn't precise enough? do you mind saying what you need this sort of large number with super precision for? – greedybuddha May 25 '13 at 16:22
  • 1
    @Jonathan That it _usually_ would not produce an integer is the reason (well, one of the reasons at least) that such a function is not provided by the standard libraries. So in your application, `base` is always a `k`-th power, and you want to raise it to the `n/k`-th power? `n`-th power exists, so you need a `k`-th root. I don't think `BigInteger` directly provides one, but it might be provided by some library out there. If not, that's much easier to write than arbitrary `double` exponents. – Daniel Fischer May 25 '13 at 16:28
  • .NET also has BigInteger, http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx – konjac May 25 '13 at 16:28
  • As far as I can tell from looking through the API docs of `BigInteger` and `BigDecimal` such an operation is not available. I don’t know of nay 3rd party libraries that offer it either, but you may go searching. – Ole V.V. Nov 18 '17 at 19:09
  • `BigInteger` and its fellow `BigDecimal` are designed to give you full precision of your numbers. An integer raised to the power of a double in most cases will not yield a rational number, so full precision simply isn’t possible. So your request doesn’t *really* make sense. – Ole V.V. Nov 19 '17 at 07:43

1 Answers1

-2

try this:

BigInteger base = new BigInteger("10");
double base1 = base.doubleValue();
double dblExponent = 10;
double res = pow(base1, dblExponent);
BigDecimal result = new BigDecimal(res);

but pay attention to precision issues

EDIT: you have to import this library: import static java.lang.Math.pow;

Community
  • 1
  • 1
Paolof76
  • 889
  • 1
  • 9
  • 23
  • 2
    Thank you but that's not what I'm looking for. The result in your code cannot be bigger than Double.MaxValue. My result BigInteger would be several hundred digits long. I need to raise a BigInteger to a floating-value power. – Jonathan May 25 '13 at 16:13
  • OK so I think with JDK 1.6 it sounds impossible. From Oracle documentation: public BigDecimal pow(int n) Returns a BigDecimal whose value is (thisn), The power is computed exactly, to unlimited precision. The parameter n must be in the range 0 through 999999999, inclusive. ZERO.pow(0) returns ONE. Note that future releases may expand the allowable exponent range of this method. – Paolof76 May 25 '13 at 16:26