What does BigInteger having no limit mean?

JavaBigintegerInteger Overflow

Java Problem Overview


I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics):

> In the BigInteger class, I have no limits and there are some helpful > functions there but it is pretty depressing to convert your beautiful > code to work with the BigInteger class, specially when primitive > operators don't work there and you must use functions from this class.

I don't know what I am missing but to represent something that has no limit you would require infinite memory ? Whats is the trick here ?

Java Solutions


Solution 1 - Java

There is no theoretical limit. The BigInteger class allocates as much memory as it needs for all the bits of data it is asked to hold.

There are, however, some practical limits, dictated by the memory available. And there are further technical limits, although you're very unlikely to be affected: some methods assume that the bits are addressable by int indexes, so things will start to break when you go above Integer.MAX_VALUE bits.

Solution 2 - Java

Graham gave great answer to this question. I would like only to add that you have to be carefull with valueOf method because it is created using long parameter so the maximum value is Long.MAX_VALUE.

Solution 3 - Java

Yes its used when we need very big numbers with arbitrary precision. It's important to note that "arbitrary" precision or number of digits does not mean "unlimited": it means that the number of digits in a number or number of digits of precision in a calculation is limited by memory and/or defined limits to precision that we specify.

Solution 4 - Java

Look at the BigInteger class source code, you will see (it can be done with NetBean). A number will be represented as an int arrays. Example, 10113 will be [1, 0, 1, 1, 3] (this is not exactly what the BigInteger class does, just an example how big number module work). So, technically, its only limit will be your memory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeekView Question on Stackoverflow
Solution 1 - JavaGraham BorlandView Answer on Stackoverflow
Solution 2 - JavaAdam SznajderView Answer on Stackoverflow
Solution 3 - JavaEduardView Answer on Stackoverflow
Solution 4 - JavaVũ Mạnh CườngView Answer on Stackoverflow