The Math functions abs(int) and abs(long) return MIN_VALUE if MIN_VALUE is provided as input. This is correct and well-defined behavior, but it's counterintuitive, as many people expect the result of abs() always to be positive.
The Math class includes functions named addExact(), subtractExact(), and multiplyExact() that throw exceptions if boundary cases with integral wraparound are encountered. Perhaps there should be similar functions for abs(), possibly named absExact(), that also throw exceptions for their (single) edge cases. There would need to be overloads for int and long.
The Math class includes functions named addExact(), subtractExact(), and multiplyExact() that throw exceptions if boundary cases with integral wraparound are encountered. Perhaps there should be similar functions for abs(), possibly named absExact(), that also throw exceptions for their (single) edge cases. There would need to be overloads for int and long.
- csr for
-
JDK-8241805 add Math.absExact
- Closed
- relates to
-
JDK-8075806 divideExact is missing in java.lang.Math
- Resolved