Switch to BigInteger if necessary

Posted by fahdshariff on Stack Overflow See other posts from Stack Overflow or by fahdshariff
Published on 2010-04-06T17:48:55Z Indexed on 2010/04/06 19:13 UTC
Read the original article Hit count: 227

Filed under:
|

I am reading a text file which contains numbers in the range [1, 10^100]. I am then performing a sequence of arithmetic operations on each number. I would like to use a BigInteger only if the number is out of the int/long range. One approach would be to count how many digits there are in the string and switch to BigInteger if there are too many. Otherwise I'd just use primitive arithmetic as it is faster. Is there a better way?

Is there any reason why Java could not do this automatically i.e. switch to BigInteger if an int was too small? This way we would not have to worry about overflows.

© Stack Overflow or respective owner

Related posts about java

Related posts about biginteger