我需要计算$a^b$
,其中a
是BigInteger
, b
是double
是否有可能在Java中这样做?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.