我已經經歷了Apache的普通數學庫Link溢出檢測減去2個多頭
在下面的代碼片段線A,B時,C沒有道理給我。有人可以對此有所瞭解嗎?
public static long subAndCheck(long a, long b) throws MathArithmeticException {
long ret;
if (b == Long.MIN_VALUE) { ----------A
if (a < 0) { --------------------B
ret = a - b; ----------------C
} else {
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
}
} else {
// use additive inverse
ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
}
return ret;
}
private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException {
final long result = a + b;
if (!((a^b) < 0 || (a^result) >= 0)) {
throw new MathArithmeticException(pattern, a, b);
}
return result;
}
檢查此問題http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo#3001879 –