[英]How to handle addition and subtraction beyond Integers MAX_VALUE and MIN_VALUE?
The following is the piece of code I am trying to implement:以下是我要实现的一段代码:
if (n1 > 0 && n2 > 0 && result >= Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
}
else if (n1 > 0 && n2 > 0 && (result <= Integer.MIN_VALUE || result < 0)) {
result = Integer.MAX_VALUE;
}
else if (n1 < 0 && n2 < 0 && (result <= Integer.MIN_VALUE || result == 0)) {
result = Integer.MIN_VALUE;
}
but I am not getting satisfactory results.但我没有得到令人满意的结果。 For example, -2147483640-10 gives me 2147483646.例如,-2147483640-10 给我 2147483646。
I am sure there has to be a more concrete way of doing saturation.我确信必须有一种更具体的方法来进行饱和。
If you need to set limits to Integer.MAX_VALUE
and Integer.MIN_VALUE
in case of overflow, you should track if sign of the result has changed to define when the overflow has taken place.如果您需要在溢出的情况下为Integer.MAX_VALUE
和Integer.MIN_VALUE
设置限制,您应该跟踪结果的符号是否已更改以定义溢出发生的时间。
Unless result
is long
, there's no need to check conditions like result >= Integer.MAX_VALUE
in case of positive overflow or result <= Integer.MAX_VALUE
for negative overflow.除非result
是long
,否则不需要检查result >= Integer.MAX_VALUE
等条件以防正溢出或result <= Integer.MAX_VALUE
用于负溢出。
public static int add(int n1, int n2) {
System.out.printf("%d + %d = ", n1, n2);
int result = n1 + n2;
if (n1 > 0 && n2 > 0 && result < 0) {
result = Integer.MAX_VALUE;
} else if (n1 < 0 && n2 < 0 && result > 0) {
result = Integer.MIN_VALUE;
}
return result;
}
Tests:测试:
System.out.println(add(10, 20));
System.out.println(add(2147483640, 10));
System.out.println(add(-10, -20));
System.out.println(add(-2147483640, -10));
Output: Output:
10 + 20 = 30
2147483640 + 10 = 2147483647
-10 + -20 = -30
-2147483640 + -10 = -2147483648
It can be done as simply as:它可以简单地完成:
return Math.min(Math.max((long) n1 + n2, Integer.MIN_VALUE), Integer.MAX_VALUE);
The operation (long) n1 + n2
ensures that the result is a long
so that n1 + n2
neither overflows nor underflows .操作(long) n1 + n2
确保结果是long
,因此n1 + n2
既不会溢出也不会下溢。
The Math.max((long) n1 + n2, Integer.MIN_VALUE)
ensure that in the case n1 + n2
would have underflow we get the value Integer.MIN_VALUE
. Math.max((long) n1 + n2, Integer.MIN_VALUE)
确保在n1 + n2
下溢的情况下,我们得到值Integer.MIN_VALUE
。 Otherwise, we get the result of n1 + n2
.否则,我们得到n1 + n2
的结果。
Finally, Math.min(.., Integer.MAX_VALUE)
ensures that if n1 + n2
would have overflows the method returns Integer.MAX_VALUE
.最后, Math.min(.., Integer.MAX_VALUE)
确保如果n1 + n2
溢出,该方法将返回Integer.MAX_VALUE
。 Otherwise, the operation n1 + n2
will be returned instead.否则,将返回操作n1 + n2
。
Running example:运行示例:
public class UnderOver {
public static long add(int n1, int n2){
return Math.min(Math.max((long) n1 + n2, Integer.MIN_VALUE), Integer.MAX_VALUE);
}
public static void main(String[] args) {
System.out.println(add(Integer.MAX_VALUE, 10));
System.out.println(add(Integer.MIN_VALUE, -10));
System.out.println(add(-10, -10));
System.out.println(add(10, 10));
System.out.println(add(10, 0));
System.out.println(add(-20, 10));
}
}
OUTPUT : OUTPUT :
2147483647
-2147483648
-20
20
10
-10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.