我正在尝试进行BigInteger指数计算。为了进行计算,我试图从m2中减去m1(均为BigInteger)并得到一个正值,但是如果将此正数转换为int数,最终会得到一个负值。
BigInteger m1 = new BigInteger("2905012217");
BigInteger m2 = new BigInteger("534500746");
int exp = m2.subtract(m1).abs().intValue();
System.out.println(exp);
BigInteger g1 = new BigInteger("2");
BigInteger output_test_g1 = g1.pow(exp);
输出:
m1:2905012217
m2:534500746
exp:-1924455825
错误:
Exception in thread "main" java.lang.ArithmeticException: Negative exponent
at java.base/java.math.BigInteger.pow(BigInteger.java:2401)
at FHEv1.homomorphicEqualityTest(FHEv1.java:195)
at FHEv1.main(FHEv1.java:323)
答案 0 :(得分:0)
整数溢出。 How does Java handle integer underflows and overflows and how would you check for it?
太高并溢出到Integer.MIN_VALUE
,如果需要则继续计数。太低,请转到Integer.MAX_VALUE
,然后继续计数(如果需要)。
例如
int A = Integer.MAX_VALUE;
A++;
//A should be Integer.MIN_VALUE, correct me if I am wrong