Java中的abs函数
一般来说调用都是没有问题的。
但是当调用
Math.abs(-2147483648)时返回的是负数,因为超过了int的表示范围,就像注释里说的
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.
解决办法就是在里面将它转成long类型的就好。
所以当调用abs函数时一定要注意参数的范围,有没有可能是最小负数。

浙公网安备 33010602011771号