I need to calculate the $a^b$
where a
is BigInteger
and b
is double
is it possible to do so in java?
Asked
Active
Viewed 187 times
0

Youcef LAIDANI
- 55,661
- 15
- 90
- 140

Karen
- 3
- 2
-
2Possible duplicate: https://stackoverflow.com/questions/16751167/can-i-raise-a-java-biginteger-to-a-double-float-power – ozanonurtek Nov 18 '17 at 18:45
-
1Raising an integer to fractional powers results in a real number. You want to get a `BigDouble` as a result. – Makoto Nov 18 '17 at 18:48
-
Possible duplicate of [Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?](https://stackoverflow.com/questions/16441769/javas-bigdecimal-powerbigdecimal-exponent-is-there-a-java-library-that-does) – sinclair Nov 18 '17 at 19:01
-
thank you I read the previous question and answers, there is no clear answer is it allowed or not in java. Thank you once again – Karen Nov 18 '17 at 19:08
1 Answers
2
Looks like the standard Java library does not provide any way to compute powers with arbitraty precision.
If you don't really need arbitrary precision and double precision is enough, you could use Math.pow(double, double)
.
If your exponent is actually an integer, you could try BigInteger.pow(int)
:
BigInteger result = new BigInteger("312413431431431431434314134").pow(5);
And if you actually need arbitraty precision power operation, you'll have to look at some library. For example, there is Apfloat: https://github.com/mtommila/apfloat
You could do something like this:
BigDecimal base = new BigDecimal("12341341341341341341341343414134134");
double exponent = 1.5;
Apfloat apfloatBase = new Apfloat(base);
Apfloat apfloatExponent = new Apfloat(exponent);
Apfloat result = ApfloatMath.pow(apfloatBase, apfloatExponent);
Javadocs: http://www.apfloat.org/apfloat_java/docs/org/apfloat/Apfloat.html http://www.apfloat.org/apfloat_java/docs/org/apfloat/ApfloatMath.html

Roman Puchkovskiy
- 11,415
- 5
- 36
- 72