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?
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.
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.
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.
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.
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-2
Integer.MAX_VALUE
(exclusive) to+2
Integer.MAX_VALUE
(exclusive) and may support values outside of that range.Implementation note:
BigInteger
constructors and operations throwArithmeticException
when the result is out of the supported range of-2
Integer.MAX_VALUE
(exclusive) to+2
Integer.MAX_VALUE
(exclusive).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With