Java – switch to BigInteger if necessary

I am reading a text file containing numbers in the range [1,10 ^ 100] Then I perform a series of arithmetic operations on each number I want to use BigInteger only when the number is outside the int / long range One method is to calculate the number of digits in the string. If there are too many digits, switch to BigInteger Otherwise I just use the original algorithm because it's faster Is there a better way?

Is there any reason why Java cannot automatically perform this operation, that is, switch to BigInteger if int is too small? So we don't have to worry about spillage

Solution

I suspect that using raw values for integer and real number decisions (for performance reasons) makes this option impossible Please note that both Python and Ruby can meet your requirements

In this case, dealing with small special cases may require more work (you need some custom classes to deal with both cases), and you should only use BigInteger

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>