--- old/src/java.base/share/classes/java/lang/Math.java 2020-03-29 11:37:16.916467092 -0700 +++ new/src/java.base/share/classes/java/lang/Math.java 2020-03-29 11:37:16.588467092 -0700 @@ -1362,6 +1362,7 @@ * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. + * @see Math#absExact(int) */ @HotSpotIntrinsicCandidate public static int abs(int a) { @@ -1369,6 +1370,32 @@ } /** + * Returns the mathematical absolute value of an {@code int} value + * if it is exactly representable as an {@code int}, throwing + * {@code ArithmeticException} if the result overflows the + * positive {@code int} range. + * + *

Since the range of two's complement integers is asymmetric + * with one additional negative value, the mathematical absolute + * value of {@link Integer#MIN_VALUE} overflows the positive + * {@code int} range, so an exception is thrown for that argument. + * + * @param a the argument whose absolute value is to be determined + * @return the absolute value of the argument, unless overflow occurs + * @throws ArithmeticException if the argument is {@link Integer#MIN_VALUE} + * @see Math#abs(int) + * @since 15 + * @see Math#absExact(long) + */ + public static int absExact(int a) { + if (a == Integer.MIN_VALUE) + throw new ArithmeticException( + "Overflow to represent absolute value of Integer.MIN_VALUE"); + else + return abs(a); + } + + /** * Returns the absolute value of a {@code long} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. @@ -1387,6 +1414,31 @@ } /** + * Returns the mathematical absolute value of an {@code long} value + * if it is exactly representable as an {@code long}, throwing + * {@code ArithmeticException} if the result overflows the + * positive {@code long} range. + * + *

Since the range of two's complement integers is asymmetric + * with one additional negative value, the mathematical absolute + * value of {@link Long#MIN_VALUE} overflows the positive + * {@code long} range, so an exception is thrown for that argument. + * + * @param a the argument whose absolute value is to be determined + * @return the absolute value of the argument, unless overflow occurs + * @throws ArithmeticException if the argument is {@link Long#MIN_VALUE} + * @see Math#abs(long) + * @since 15 + */ + public static long absExact(long a) { + if (a == Long.MIN_VALUE) + throw new ArithmeticException( + "Overflow to represent absolute value of Long.MIN_VALUE"); + else + return abs(a); + } + + /** * Returns the absolute value of a {@code float} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned.