【发布时间】:2015-04-09 19:22:19
【问题描述】:
我正在研究一种算法来检查数字是否为素数并且需要处理非常大的数字,因此我正在使用 BigInteger 类。问题是抛出此异常ArithmeticException BigInteger 会溢出支持的范围。
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(Unknown Source)
at java.math.BigInteger.checkRange(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
at java.math.BigInteger.shiftLeft(Unknown Source)
at java.math.BigInteger.pow(Unknown Source)
at Kitas.main(Kitas.java:118)
以及抛出异常的那一行:
b = BigInteger.valueOf(2).pow((int) (35*(Math.pow(2, counter))));
一旦计数器的值达到 26,就会引发异常。
【问题讨论】:
标签: java range overflow biginteger arithmeticexception