mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8075806: divideExact is missing in java.lang.Math
Reviewed-by: darcy
This commit is contained in:
parent
efa63dc1c6
commit
0b12e7c82c
3 changed files with 167 additions and 20 deletions
|
@ -66,13 +66,8 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
|
|||
* will not overflow the range of values of the computation.
|
||||
* The best practice is to choose the primitive type and algorithm to avoid
|
||||
* overflow. In cases where the size is {@code int} or {@code long} and
|
||||
* overflow errors need to be detected, the methods {@code addExact},
|
||||
* {@code subtractExact}, {@code multiplyExact}, {@code toIntExact},
|
||||
* {@code incrementExact}, {@code decrementExact} and {@code negateExact}
|
||||
* throw an {@code ArithmeticException} when the results overflow.
|
||||
* For the arithmetic operations divide and absolute value, overflow
|
||||
* occurs only with a specific minimum or maximum value and
|
||||
* should be checked against the minimum or maximum as appropriate.
|
||||
* overflow errors need to be detected, the methods whose names end with
|
||||
* {@code Exact} throw an {@code ArithmeticException} when the results overflow.
|
||||
*
|
||||
* <h2><a id=Ieee754RecommendedOps>IEEE 754 Recommended
|
||||
* Operations</a></h2>
|
||||
|
@ -858,6 +853,54 @@ public final class StrictMath {
|
|||
return Math.multiplyExact(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the quotient of the arguments, throwing an exception if the
|
||||
* result overflows an {@code int}. Such overflow occurs in this method if
|
||||
* {@code x} is {@link Integer#MIN_VALUE} and {@code y} is {@code -1}.
|
||||
* In contrast, if {@code Integer.MIN_VALUE / -1} were evaluated directly,
|
||||
* the result would be {@code Integer.MIN_VALUE} and no exception would be
|
||||
* thrown.
|
||||
* <p>
|
||||
* If {@code y} is zero, an {@code ArithmeticException} is thrown
|
||||
* (JLS {@jls 15.17.2}).
|
||||
*
|
||||
* @param x the dividend
|
||||
* @param y the divisor
|
||||
* @return the quotient {@code x / y}
|
||||
* @throws ArithmeticException if {@code y} is zero or the quotient
|
||||
* overflows an int
|
||||
* @jls 15.17.2 Division Operator /
|
||||
* @see Math#divideExact(int,int)
|
||||
* @since 18
|
||||
*/
|
||||
public static int divideExact(int x, int y) {
|
||||
return Math.divideExact(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the quotient of the arguments, throwing an exception if the
|
||||
* result overflows a {@code long}. Such overflow occurs in this method if
|
||||
* {@code x} is {@link Long#MIN_VALUE} and {@code y} is {@code -1}.
|
||||
* In contrast, if {@code Long.MIN_VALUE / -1} were evaluated directly,
|
||||
* the result would be {@code Long.MIN_VALUE} and no exception would be
|
||||
* thrown.
|
||||
* <p>
|
||||
* If {@code y} is zero, an {@code ArithmeticException} is thrown
|
||||
* (JLS {@jls 15.17.2}).
|
||||
*
|
||||
* @param x the dividend
|
||||
* @param y the divisor
|
||||
* @return the quotient {@code x / y}
|
||||
* @throws ArithmeticException if {@code y} is zero or the quotient
|
||||
* overflows a long
|
||||
* @jls 15.17.2 Division Operator /
|
||||
* @see Math#divideExact(long,long)
|
||||
* @since 18
|
||||
*/
|
||||
public static long divideExact(long x, long y) {
|
||||
return Math.divideExact(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the argument incremented by one,
|
||||
* throwing an exception if the result overflows an {@code int}.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue