简体   繁体   English

Java ArithmeticException BigInteger会溢出支持的范围

[英]Java ArithmeticException BigInteger would overflow supported range

I am working on a algorithm to check if number is prime and need to work with really big numbers therefore I am using BigInteger class. 我正在研究一种算法,以检查数字是否为质数,并且需要处理非常大的数字,因此我正在使用BigInteger类。 Problem is that this exception is thrown ArithmeticException BigInteger would overflow supported range . 问题是抛出此异常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)

And the line where exception is thrown: 和抛出异常的行:

b = BigInteger.valueOf(2).pow((int) (35*(Math.pow(2, counter))));

Once counter reaches value of 26, an exception is thrown. 一旦计数器的值达到26,就会引发异常。

(int) (35 * Math.pow(2, 26)) == (int) (2348810240d) = Integer.MAX_VALUE

with the result that the power you're trying to raise 2 to is Integer.MAX_VALUE, so the result would have over Integer.MAX_VALUE binary digits. 结果您尝试将2的幂提高为Integer.MAX_VALUE,因此结果将超过Integer.MAX_VALUE二进制数字。 BigInteger isn't big enough for that, and storing numbers that large is pretty impractical. BigInteger不够大,并且存储如此大的数字是不切实际的。

Nothing built into Java will let you test primality of numbers that large. Java内置的任何功能都无法让您测试那么大的数字的素数。

BigInteger uses an int[] in order to store the value of the array. BigInteger使用int[]来存储数组的值。 This means that the number cannot be larger than 2^(Integer.Max_Value), as anything larger than that would make the index of the array (stored in a single int) to be larger than the maximum size of the array. 这意味着该数字不能大于2 ^(Integer.Max_Value),因为任何大于该值的值都会使数组的索引(存储在单个int中)大于数组的最大大小。

at 26, the number you are storing is: 在26,您要存储的号码是:

2^(35*[2^26]) = 2^2348810240 

the power of two used here (2,348,810,240) is slightly larger than (2^31-1), which is the maximum value that can be stored in a BigInteger due to the implementation of the BigInteger's internal storage. 此处使用的2的幂(2,348,810,240)略大于(2 ^ 31-1),这是由于实现了BigInteger的内部存储而可以存储在BigInteger中的最大值。 Going above a counter of 26 only makes this problem worse. 超过26的计数器只会使这个问题更糟。

If you really need to work with numbers this large you may have to write your own version of BigInteger, which uses something else to store its values, which allows more storage space. 如果您确实需要使用如此大的数字,则可能必须编写自己的BigInteger版本,该版本使用其他方式存储其值,从而允许更多的存储空间。 Maybe a 2-D array like so: int[][] storage , as that could hold values up to 2^(2^(2^32-1)-1). 也许像这样的二维数组: int[][] storage ,因为它可以保存最大2 ^(2 ^(2 ^ 32-1)-1)的值。 If you need more than that, you can keep increasing the array's dimensions until you run out of memory on your computer - if completely filling an int[][] won't already do that (I suspect it will). 如果您需要的更多,则可以继续增加数组的大小,直到计算机内存用完为止-如果完全填充int [] []不会那么做(我怀疑是这样)。

See documentation for more info. 请参阅文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM