Is there an upper bound to BigInteger?

asteri picture asteri · Oct 2, 2012 · Viewed 116.1k times · Source

Possible Duplicate:
What does BigInteger having no limit mean?

The Javadoc for BigInteger does not define any maximum or minimum. However, it does say:

(emphasis added)

Immutable arbitrary-precision integers

Is there such a maximum, even in theory? Or is the way BigInteger operates fundamentally different, such that there is in reality no maximum except for the amount of memory available on the computer?

Answer

assylias picture assylias · Oct 2, 2012

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

Admittedly, this is implementation dependent, not part of the specification.


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

BigInteger must support values in the range -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive) and may support values outside of that range.

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