BigInteger的负指数

时间:2015-01-07 08:54:48

标签: java math biginteger modulus

我在Java中执行一些算术运算并遇到使用BigInteger执行负指数幂的问题(用于导出模数反转)。

BigInteger x = new BigInteger("6");
x.pow(-1);

当然,这导致了错误:

Exception in thread "main" java.lang.ArithmeticException: Negative exponent

理解modpow允许负指数,但我不是在我的情况下使用mod。有没有可能扭转来纠正这个问题?

1 个答案:

答案 0 :(得分:0)

如果情况变得更糟,你可以随时使用微积分的Maclaurin幂系列。

的Maclaurin系列
   e^-x = 1 - x + (1/2!)x^2 - (1/3!)x^3 + ...

可以与BigDecimal一起用来获得e ^ -x的答案。

相关问题