JDK-8271225 : Add floorDivExact() method to java.lang.[Strict]Math
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 18
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2021-07-23
  • Updated: 2021-09-02
  • Resolved: 2021-08-31
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 18
18 b13Fixed
Related Reports
Blocks :  
CSR :  
Relates :  
Description
JDK-8270378 added Math.divideExact() for symmetry with multiplyExact() and to account for the special cases of the division operator '/' where Integer.MIN_VALUE / -1 == Integer.MIN_VALUE and Long.MIN_VALUE / -1 == Long.MIN_VALUE. The same special case also occurs for floorDiv(int,int) as stated in its specification:

"Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. There is one special case, if the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Integer.MIN_VALUE."

The analogous special case obtains as well for floorDiv(long,long). Instead of ignoring the numeric overflow, a method floorDivExact() would throw an ArithmeticException when the dividend is the respective MIN_VALUE and the divisor is -1.
Comments
Changeset: e5518528 Author: Brian Burkhalter <bpb@openjdk.org> Date: 2021-08-31 14:42:14 +0000 URL: https://git.openjdk.java.net/jdk/commit/e55185280126e450e31eb65aa8342aebe6f31606
31-08-2021

As part of this fix, it would be worth noting at the appropriate places in the java.lang.Math specification that '%' is a suitable remainder counterpart for divideExact() and that floorMod() is a suitable modulus counterpart for floorDivExact().
23-07-2021