java整数除法看似简单,但是仔细考虑就会发现一些细节并不是显而易见的。例如,-9/4结果是-2还是-3?
查阅了《java编程思想》(第四版 机械工业出版社 陈昊鹏译 41页):
整数除法会直接去掉结果的小数位,而不是四舍五入地圆整结果。
以及《Java 核心技术 卷I》(第十版 机械工业出版社 周立新等译 38页):
当与/运算的两个操作数都是整数时,表示整数除法
都没有明确的说明具体的预期结果。因此,从网上查阅了java的specifiction15.17.2. Division Operator /
The binary
/
operator performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor.
二元操作符 /
对操作数进行除法运算,得到二者之商。左边的操作符是被除数,右边的操作数是除数。
Integer division rounds toward
0
. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d ⋅ q| ≤ |n|. Moreover, q is positive when |n| ≥ |d| and n and d have the same sign, but q is negative when |n| ≥ |d| and n and d have opposite signs.
整数除法在 0
附近取整。也就是说对于整数 n 和 d,他们的商是满足以下条件的具有最大magnitude
(我理解是绝对值)的整数
|d ⋅ q| ≤ |n|
进一步说,当 |n| ≥ |d| 并且 n 和 d 的符号相同的时候,q 为正;当 |n| ≥ |d| 并且 n 和 d 的符号不同的时候,q 为负。
这里还有隐含一层的意思,即当 |n| < |d| 的时候,q 为 0。
There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is
-1
, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case. On the other hand, if the value of the divisor in an integer division is0
, then anArithmeticException
is thrown.
有一个特例不满足这一法则:如果被除数为负,且其绝对值为所有同类型负数中的最大值,在除数为 -1 的情况下,如果按照上述规则,会发生溢出,因此此时输出和被除数相同。尽管发生了溢出,也不会有异常抛出。另一方面,如果除数为 0
, 就会抛出 ArithmeticException
。
代码演示:
private static void test1(){
System.out.println(9/4);
System.out.println(9/-4);
System.out.println(-9/4);
System.out.println(-9/-4);
System.out.println(4/9);
System.out.println(-4/9);
System.out.println(4/-9);
System.out.println(-4/-9);
System.out.println(Integer.MIN_VALUE/-1);
}
输出
2
-2
-2
2
0
0
0
0
-2147483648