java number exceeds long.max_value - how to detect?

喜你入骨 提交于 2019-12-01 04:51:28

If you can't be sure the result will be less than 9 trillion trillion, I would use double or BigInteger Getting an error doesn't help you very much because you still need to know what to do about.

Much better that you don't get an error in the first place by validating your input to ensure they are in range and if the range of the result is larger than long use a type which can handle this.

With BigInteger you can do

BigInteger a = BigInteger.valueOf(2).multiply(BigInteger.valueOf(Long.MAX_VALUE));
long l = a.longValue();
if (a.compareTo(BigInteger.valueOf(l)) == 0) {
    // ok
} else {
    // error
}

With double you can do

double d = 2.0 * Long.MAX_VALUE;
long l = (long) Math.max(Long.MIN_VALUE, Math.min(Long.MAX_VALUE, d));
// or as a helper method.
long l = boundedCast(d);

Note: using double instead of long can result in some loss of precision.

I would prefer to avoid the need for an error block in the first place.

Exceding the maximum value of a long doesnt throw an exception, instead it cicles back. If you do this:

Long.MAX_VALUE + 1

you will notice that the result is the equivalent to Long.MIN_VALUE.

If you want it to throw an exception check if it reached the max value and throw the exception

[Edit]

You can also use the Guava Library to check if there is an overflow when you sum two longs;

long c = LongMath.checkedAdd(a, b);

this throws an exception when an overflow occurs while summing two longs.

You can find the javadoc here

Long values exceeding MAX_VALUE doesn't throw any exception. You need to check and handle such situations manually.

ALthough as @PeterLawrey suggested you should consider using double and BigInteger.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!