# HG changeset patch # User bpb # Date 1443572924 25200 # Node ID 43fec67d51a30739d999a07e94b6f4e9bd29b7a5 # Parent f196252337c7395aa4f343fabe1e22013f4a8e88 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math Summary: Add new methods with long, int signatures. Reviewed-by: darcy, scolebourne diff -r f196252337c7 -r 43fec67d51a3 jdk/src/java.base/share/classes/java/lang/Math.java --- a/jdk/src/java.base/share/classes/java/lang/Math.java Fri May 20 15:50:20 2016 -0700 +++ b/jdk/src/java.base/share/classes/java/lang/Math.java Tue Sep 29 17:28:44 2015 -0700 @@ -95,7 +95,7 @@ * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact} * throw an {@code ArithmeticException} when the results overflow. * For other arithmetic operations such as divide, absolute value, - * increment, decrement, and negation overflow occurs only with + * increment by one, decrement by one, and negation, overflow occurs only with * a specific minimum or maximum value and should be checked against * the minimum or maximum as appropriate. * @@ -861,7 +861,7 @@ public static int subtractExact(int x, int y) { int r = x - y; // HD 2-12 Overflow iff the arguments have different signs and - // the sign of the result is different than the sign of x + // the sign of the result is different from the sign of x if (((x ^ y) & (x ^ r)) < 0) { throw new ArithmeticException("integer overflow"); } @@ -882,7 +882,7 @@ public static long subtractExact(long x, long y) { long r = x - y; // HD 2-12 Overflow iff the arguments have different signs and - // the sign of the result is different than the sign of x + // the sign of the result is different from the sign of x if (((x ^ y) & (x ^ r)) < 0) { throw new ArithmeticException("long overflow"); } @@ -909,6 +909,20 @@ } /** + * Returns the product of the arguments, throwing an exception if the result + * overflows a {@code long}. + * + * @param x the first value + * @param y the second value + * @return the result + * @throws ArithmeticException if the result overflows a long + * @since 1.9 + */ + public static long multiplyExact(long x, int y) { + return multiplyExact(x, (long)y); + } + + /** * Returns the product of the arguments, * throwing an exception if the result overflows a {@code long}. * @@ -1112,12 +1126,12 @@ * There is one special case, if the dividend is the * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, * then integer overflow occurs and - * the result is equal to the {@code Integer.MIN_VALUE}. + * the result is equal to {@code Integer.MIN_VALUE}. *

* Normal integer division operates under the round to zero rounding mode * (truncation). This operation instead acts under the round toward * negative infinity (floor) rounding mode. - * The floor rounding mode gives different results than truncation + * The floor rounding mode gives different results from truncation * when the exact result is negative. *