A DESCRIPTION OF THE REQUEST :
For instance Integer.MIN_VALUE / -1 will overflow, but there is no method divideExact in java.lang.Math. A standardized divideExact would be most helpful, because it would allow to write programs which use well defined methods and shows the overflow is critical without the need to clutter it with (probable undocumented) if statements for this special case.
JUSTIFICATION :
addExact, subtractExact, multiplyExact, incrementExact, decrementExact, and negateExact have been added to java.lang.Math to support arithmetic operations which support overflow detection (even though the documentation doesn't say this for the latter three in the Math documentation). This might suggest that there is no overflow risk with the divide operation. Alone for symmetric reasons the divideExact method should be added.
Additionally the JVM might be able to optimize this method by using a single processor instruction.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Add the following methods to java.lang.Math
int divideExact(int, int)
long divideExact(long, long)
ACTUAL -
The requested methods don't exist.
CUSTOMER SUBMITTED WORKAROUND :
int x = â¦, y = â¦, z;
if (x == Integer.MIN_VALUE && y == -1) {
throw new ArithmeticException("integer overflow");
}
z = x / y;
// The other case y == 0 has been omitted, because it is already covered by the JVM
// (with no special case for 0/0).
// See also: Henry S. Warren, Jr.:Hacker's Delight (2nd Ed.), Page 34.
For instance Integer.MIN_VALUE / -1 will overflow, but there is no method divideExact in java.lang.Math. A standardized divideExact would be most helpful, because it would allow to write programs which use well defined methods and shows the overflow is critical without the need to clutter it with (probable undocumented) if statements for this special case.
JUSTIFICATION :
addExact, subtractExact, multiplyExact, incrementExact, decrementExact, and negateExact have been added to java.lang.Math to support arithmetic operations which support overflow detection (even though the documentation doesn't say this for the latter three in the Math documentation). This might suggest that there is no overflow risk with the divide operation. Alone for symmetric reasons the divideExact method should be added.
Additionally the JVM might be able to optimize this method by using a single processor instruction.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Add the following methods to java.lang.Math
int divideExact(int, int)
long divideExact(long, long)
ACTUAL -
The requested methods don't exist.
CUSTOMER SUBMITTED WORKAROUND :
int x = â¦, y = â¦, z;
if (x == Integer.MIN_VALUE && y == -1) {
throw new ArithmeticException("integer overflow");
}
z = x / y;
// The other case y == 0 has been omitted, because it is already covered by the JVM
// (with no special case for 0/0).
// See also: Henry S. Warren, Jr.:Hacker's Delight (2nd Ed.), Page 34.
- csr for
-
JDK-8270378 divideExact is missing in java.lang.Math
-
- Closed
-
- relates to
-
JDK-8241374 add Math.absExact
-
- Resolved
-
-
JDK-8271225 Add floorDivExact() method to java.lang.[Strict]Math
-
- Resolved
-
-
JDK-6708398 Support integer overflow
-
- Closed
-
-
JDK-8023217 Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
-
- Resolved
-
(2 links to)