Is there an upper bound for BigInteger?

Possible duplicate:
What does BigInteger mean without limitation?

Javadoc for BigInteger does not define a maximum or minimum. However, he says:

(in italics)

Immutable integers of arbitrary precision

Is there such a maximum, even in theory? Or does the BigInteger method work in a completely different way, so there really is no maximum other than the amount of memory available on the computer?

+57
java biginteger
Oct 02
source share
3 answers

The number is stored in int[] - the maximum size of the array is Integer.MAX_VALUE . So the maximum BigInteger is probably (2 ^ 32) ^ Integer.MAX_VALUE .

Admittedly, this is implementation dependent, not specification.




In Java 8, some information was added to the BigInteger javadoc , providing the minimum supported range and the actual limit of the current implementation:

BigInteger must support values โ€‹โ€‹in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exception) and may support values โ€‹โ€‹outside this range.

Implementation note: constructors and operations of BigInteger throw ArithmeticException when the result falls outside the supported range of -2 Integer.MAX_VALUE (exception) to +2 Integer.MAX_VALUE (exclusive).

+48
Oct 02
source share

BigInteger will only be used if you know that it will not be decimal and there is a possibility that the long data type will not be large enough. BigInteger has no restrictions on the maximum size (the size of RAM on a computer can hold).

From here .

It is implemented using int[] :

  110 /** 111 * The magnitude of this BigInteger, in <i>big-endian</i> order: the 112 * zeroth element of this array is the most-significant int of the 113 * magnitude. The magnitude must be "minimal" in that the most-significant 114 * int ({@code mag[0]}) must be non-zero. This is necessary to 115 * ensure that there is exactly one representation for each BigInteger 116 * value. Note that this implies that the BigInteger zero has a 117 * zero-length mag array. 118 */ 119 final int[] mag; 

From source

From the Wikipedia article Arithmetic with arbitrary precision :

Several modern programming languages โ€‹โ€‹have built-in support for bignums and others have libraries available for arbitrary precision integer and floating point. Instead of storing the values โ€‹โ€‹as a fixed number of binary bits associated with the size of the processor register, these implementations usually use arrays of variable length digits.

+15
Oct 02
source share

The first maximum you hit is the length of the string, which is 2 31 -1 digits. It is much smaller than the BigInteger maximum, but IMHO it loses most of its value if it cannot be printed.

+10
Oct 02
source share



All Articles