Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an upper bound to BigInteger? [duplicate]

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?

like image 242
asteri Avatar asked Oct 02 '12 15:10

asteri


People also ask

How many digits can BigInteger hold?

Now that we are able to represent numerical numbers using Strings, we have raised the maximum number we can initialize a big integer to a number with 2147483647 digits. This is because the maximum length of a String is Integer. MAX_VALUE.

What is the upper limit on the size of an integer represented by the BigInteger object in Java?

The BigInteger class allows you to create and manipulate integer numbers of any size. The BigInteger class stores a number as an array of unsigned, 32-bit integer "digits" with a radix, or base, of 4294967296.

How do you cast BigInteger to double?

math. BigInteger. doubleValue() converts this BigInteger to a double value. If the value return by this function is too big for a magnitude to represent as a double then it will be converted to Double.

Can BigInteger overflow?

Though variables of type long can also overflow, the minimum and maximum values are much larger and are probably sufficient in most situations. The value range of BigInteger is not restricted, except by the amount of memory available to the JVM. As we can see in the output, there's no overflow here.


2 Answers

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).

like image 160
assylias Avatar answered Sep 25 '22 16:09

assylias


BigInteger would only be used if you know it will not be a decimal and there is a possibility of the long data type not being large enough. BigInteger has no cap on its max size (as large as the RAM on the computer can hold).

From here.

It is implemented using an 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 the source

From the Wikipedia article Arbitrary-precision arithmetic:

Several modern programming languages have built-in support for bignums, and others have libraries available for arbitrary-precision integer and floating-point math. Rather than store values as a fixed number of binary bits related to the size of the processor register, these implementations typically use variable-length arrays of digits.

like image 26
embedded.kyle Avatar answered Sep 21 '22 16:09

embedded.kyle