JDK-8241374 : add Math.absExact
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-03-20
  • Updated: 2021-07-13
  • Resolved: 2020-03-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15 b18Fixed
Related Reports
CSR :  
Relates :  
Description
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.
Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/04b9012767e2 User: darcy Date: 2020-03-30 20:49:15 +0000
30-03-2020

Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2020-March/065504.html
28-03-2020

+ 20_000 on this one because 1) as you said abs(MIN_VALUE) is an overflow 2) it raises awareness of the semantics of Math.abs()
20-03-2020