Mercurial > hg > openjdk > jdk8 > jdk
changeset 6097:cdf02b372956
6282196: There should be Math.mod(number, modulo) methods
Summary: added the requested methods
Reviewed-by: darcy, emcmanus, alanb
Contributed-by: roger.riggs@oracle.com
author | sherman |
---|---|
date | Wed, 07 Nov 2012 20:50:09 -0800 |
parents | 599f231cba97 |
children | 1e7dd9e05ce2 |
files | src/share/classes/java/lang/Math.java src/share/classes/java/lang/StrictMath.java test/java/lang/Math/DivModTests.java |
diffstat | 3 files changed, 665 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/lang/Math.java Wed Nov 07 17:39:34 2012 -0800 +++ b/src/share/classes/java/lang/Math.java Wed Nov 07 20:50:09 2012 -0800 @@ -742,6 +742,7 @@ * @param y the second value * @return the result * @throws ArithmeticException if the result overflows an int + * @since 1.8 */ public static int addExact(int x, int y) { int r = x + y; @@ -760,6 +761,7 @@ * @param y the second value * @return the result * @throws ArithmeticException if the result overflows a long + * @since 1.8 */ public static long addExact(long x, long y) { long r = x + y; @@ -778,6 +780,7 @@ * @param y the second value to subtract from the first * @return the result * @throws ArithmeticException if the result overflows an int + * @since 1.8 */ public static int subtractExact(int x, int y) { int r = x - y; @@ -797,6 +800,7 @@ * @param y the second value to subtract from the first * @return the result * @throws ArithmeticException if the result overflows a long + * @since 1.8 */ public static long subtractExact(long x, long y) { long r = x - y; @@ -816,6 +820,7 @@ * @param y the second value * @return the result * @throws ArithmeticException if the result overflows an int + * @since 1.8 */ public static int multiplyExact(int x, int y) { long r = (long)x * (long)y; @@ -833,6 +838,7 @@ * @param y the second value * @return the result * @throws ArithmeticException if the result overflows a long + * @since 1.8 */ public static long multiplyExact(long x, long y) { long r = x * y; @@ -857,6 +863,7 @@ * @param value the long value * @return the argument as an int * @throws ArithmeticException if the {@code argument} overflows an int + * @since 1.8 */ public static int toIntExact(long value) { if ((int)value != value) { @@ -866,6 +873,159 @@ } /** + * Returns the largest (closest to positive infinity) + * {@code int} value that is less than or equal to the algebraic quotient. + * 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}. + * <p> + * 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 + * when the exact result is negative. + * <ul> + * <li>If the signs of the arguments are the same, the results of + * {@code floorDiv} and the {@code /} operator are the same. <br> + * For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li> + * <li>If the signs of the arguments are different, the quotient is negative and + * {@code floorDiv} returns the integer less than or equal to the quotient + * and the {@code /} operator returns the integer closest to zero.<br> + * For example, {@code floorDiv(-4, 3) == -2}, + * whereas {@code (-4 / 3) == -1}. + * </li> + * </ul> + * <p> + * + * @param x the dividend + * @param y the divisor + * @return the largest (closest to positive infinity) + * {@code int} value that is less than or equal to the algebraic quotient. + * @throws ArithmeticException if the divisor {@code y} is zero + * @see #floorMod(int, int) + * @see #floor(double) + * @since 1.8 + */ + public static int floorDiv(int x, int y) { + int r = x / y; + // if the signs are different and modulo not zero, round down + if ((x ^ y) < 0 && (r * y != x)) { + r--; + } + return r; + } + + /** + * Returns the largest (closest to positive infinity) + * {@code long} value that is less than or equal to the algebraic quotient. + * There is one special case, if the dividend is the + * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, + * then integer overflow occurs and + * the result is equal to the {@code Long.MIN_VALUE}. + * <p> + * 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 + * when the exact result is negative. + * <p> + * For examples, see {@link #floorDiv(int, int)}. + * + * @param x the dividend + * @param y the divisor + * @return the largest (closest to positive infinity) + * {@code long} value that is less than or equal to the algebraic quotient. + * @throws ArithmeticException if the divisor {@code y} is zero + * @see #floorMod(long, long) + * @see #floor(double) + * @since 1.8 + */ + public static long floorDiv(long x, long y) { + long r = x / y; + // if the signs are different and modulo not zero, round down + if ((x ^ y) < 0 && (r * y != x)) { + r--; + } + return r; + } + + /** + * Returns the floor modulus of the {@code int} arguments. + * <p> + * The floor modulus is {@code x - (floorDiv(x, y) * y)}, + * has the same sign as the divisor {@code y}, and + * is in the range of {@code -abs(y) < r < +abs(y)}. + * + * <p> + * The relationship between {@code floorDiv} and {@code floorMod} is such that: + * <ul> + * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} + * </ul> + * <p> + * The difference in values between {@code floorMod} and + * the {@code %} operator is due to the difference between + * {@code floorDiv} that returns the integer less than or equal to the quotient + * and the {@code /} operator that returns the integer closest to zero. + * <p> + * Examples: + * <ul> + * <li>If the signs of the arguments are the same, the results + * of {@code floorMod} and the {@code %} operator are the same. <br> + * <ul> + * <li>{@code floorMod(4, 3) == 1}; and {@code (4 % 3) == 1}</li> + * </ul> + * <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br> + * <ul> + * <li>{@code floorMod(+4, -3) == -2}; and {@code (+4 % -3) == +1} </li> + * <li>{@code floorMod(-4, +3) == +2}; and {@code (-4 % +3) == -1} </li> + * <li>{@code floorMod(-4, -3) == -1}; and {@code (-4 % -3) == -1 } </li> + * </ul> + * </li> + * </ul> + * <p> + * If the signs of arguments are unknown and a positive modulus + * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}. + * + * @param x the dividend + * @param y the divisor + * @return the floor modulus {@code x - (floorDiv(x, y) * y)} + * @throws ArithmeticException if the divisor {@code y} is zero + * @see #floorDiv(int, int) + * @since 1.8 + */ + public static int floorMod(int x, int y) { + int r = x - floorDiv(x, y) * y; + return r; + } + + /** + * Returns the floor modulus of the {@code long} arguments. + * <p> + * The floor modulus is {@code x - (floorDiv(x, y) * y)}, + * has the same sign as the divisor {@code y}, and + * is in the range of {@code -abs(y) < r < +abs(y)}. + * + * <p> + * The relationship between {@code floorDiv} and {@code floorMod} is such that: + * <ul> + * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} + * </ul> + * <p> + * For examples, see {@link #floorMod(int, int)}. + * + * @param x the dividend + * @param y the divisor + * @return the floor modulus {@code x - (floorDiv(x, y) * y)} + * @throws ArithmeticException if the divisor {@code y} is zero + * @see #floorDiv(long, long) + * @since 1.8 + */ + public static long floorMod(long x, long y) { + return x - floorDiv(x, y) * y; + } + + /** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned.
--- a/src/share/classes/java/lang/StrictMath.java Wed Nov 07 17:39:34 2012 -0800 +++ b/src/share/classes/java/lang/StrictMath.java Wed Nov 07 20:50:09 2012 -0800 @@ -365,7 +365,7 @@ * @param a the value to be floored or ceiled * @param negativeBoundary result for values in (-1, 0) * @param positiveBoundary result for values in (0, 1) - * @param sign the sign of the result + * @param increment value to add when the argument is non-integral */ private static double floorOrCeil(double a, double negativeBoundary, @@ -702,7 +702,7 @@ * <p>This method is properly synchronized to allow correct use by * more than one thread. However, if many threads need to generate * pseudorandom numbers at a great rate, it may reduce contention - * for each thread to have its own pseudorandom number generator. + * for each thread to have its own pseudorandom-number generator. * * @return a pseudorandom {@code double} greater than or equal * to {@code 0.0} and less than {@code 1.0}. @@ -745,7 +745,7 @@ } /** - * Return the difference of the arguments, + * Returns the difference of the arguments, * throwing an exception if the result overflows an {@code int}. * * @param x the first value @@ -760,7 +760,7 @@ } /** - * Return the difference of the arguments, + * Returns the difference of the arguments, * throwing an exception if the result overflows a {@code long}. * * @param x the first value @@ -775,7 +775,7 @@ } /** - * Return the product of the arguments, + * Returns the product of the arguments, * throwing an exception if the result overflows an {@code int}. * * @param x the first value @@ -790,7 +790,7 @@ } /** - * Return the product of the arguments, + * Returns the product of the arguments, * throwing an exception if the result overflows a {@code long}. * * @param x the first value @@ -805,7 +805,7 @@ } /** - * Return the value of the {@code long} argument; + * Returns the value of the {@code long} argument; * throwing an exception if the value overflows an {@code int}. * * @param value the long value @@ -819,6 +819,107 @@ } /** + * Returns the largest (closest to positive infinity) + * {@code int} value that is less than or equal to the algebraic quotient. + * 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}. + * <p> + * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and + * a comparison to the integer division {@code /} operator. + * + * @param x the dividend + * @param y the divisor + * @return the largest (closest to positive infinity) + * {@code int} value that is less than or equal to the algebraic quotient. + * @throws ArithmeticException if the divisor {@code y} is zero + * @see Math#floorDiv(int, int) + * @see Math#floor(double) + * @since 1.8 + */ + public static int floorDiv(int x, int y) { + return Math.floorDiv(x, y); + } + + /** + * Returns the largest (closest to positive infinity) + * {@code long} value that is less than or equal to the algebraic quotient. + * There is one special case, if the dividend is the + * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, + * then integer overflow occurs and + * the result is equal to the {@code Long.MIN_VALUE}. + * <p> + * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and + * a comparison to the integer division {@code /} operator. + * + * @param x the dividend + * @param y the divisor + * @return the largest (closest to positive infinity) + * {@code long} value that is less than or equal to the algebraic quotient. + * @throws ArithmeticException if the divisor {@code y} is zero + * @see Math#floorDiv(long, long) + * @see Math#floor(double) + * @since 1.8 + */ + public static long floorDiv(long x, long y) { + return Math.floorDiv(x, y); + } + + /** + * Returns the floor modulus of the {@code int} arguments. + * <p> + * The floor modulus is {@code x - (floorDiv(x, y) * y)}, + * has the same sign as the divisor {@code y}, and + * is in the range of {@code -abs(y) < r < +abs(y)}. + * <p> + * The relationship between {@code floorDiv} and {@code floorMod} is such that: + * <ul> + * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} + * </ul> + * <p> + * See {@link Math#floorMod(int, int) Math.floorMod} for examples and + * a comparison to the {@code %} operator. + * + * @param x the dividend + * @param y the divisor + * @return the floor modulus {@code x - (floorDiv(x, y) * y)} + * @throws ArithmeticException if the divisor {@code y} is zero + * @see Math#floorMod(int, int) + * @see StrictMath#floorDiv(int, int) + * @since 1.8 + */ + public static int floorMod(int x, int y) { + return Math.floorMod(x , y); + } + /** + * Returns the floor modulus of the {@code long} arguments. + * <p> + * The floor modulus is {@code x - (floorDiv(x, y) * y)}, + * has the same sign as the divisor {@code y}, and + * is in the range of {@code -abs(y) < r < +abs(y)}. + * <p> + * The relationship between {@code floorDiv} and {@code floorMod} is such that: + * <ul> + * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} + * </ul> + * <p> + * See {@link Math#floorMod(int, int) Math.floorMod} for examples and + * a comparison to the {@code %} operator. + * + * @param x the dividend + * @param y the divisor + * @return the floor modulus {@code x - (floorDiv(x, y) * y)} + * @throws ArithmeticException if the divisor {@code y} is zero + * @see Math#floorMod(long, long) + * @see StrictMath#floorDiv(long, long) + * @since 1.8 + */ + public static long floorMod(long x, long y) { + return Math.floorMod(x, y); + } + + /** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. @@ -1543,7 +1644,7 @@ } /** - * Return {@code d} × + * Returns {@code d} × * 2<sup>{@code scaleFactor}</sup> rounded as if performed * by a single correctly rounded floating-point multiply to a * member of the double value set. See the Java @@ -1577,7 +1678,7 @@ } /** - * Return {@code f} × + * Returns {@code f} × * 2<sup>{@code scaleFactor}</sup> rounded as if performed * by a single correctly rounded floating-point multiply to a * member of the float value set. See the Java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/Math/DivModTests.java Wed Nov 07 20:50:09 2012 -0800 @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * @test Test Math and StrictMath Floor Div / Modulo operations. + * @bug 6282196 + * @summary Basic tests for Floor division and modulo methods for both Math + * and StrictMath for int and long datatypes. + */ +public class DivModTests { + + /** + * The count of test errors. + */ + private static int errors = 0; + + /** + * @param args the command line arguments are unused + */ + public static void main(String[] args) { + errors = 0; + testIntFloorDivMod(); + testLongFloorDivMod(); + + if (errors > 0) { + throw new RuntimeException(errors + " errors found in DivMod methods."); + } + } + + /** + * Report a test failure and increment the error count. + * @param message the formatting string + * @param args the variable number of arguments for the message. + */ + static void fail(String message, Object... args) { + errors++; + System.out.printf(message, args); + } + + /** + * Test the integer floorDiv and floorMod methods. + * Math and StrictMath tested and the same results are expected for both. + */ + static void testIntFloorDivMod() { + testIntFloorDivMod(4, 0, new ArithmeticException("/ by zero"), new ArithmeticException("/ by zero")); // Should throw ArithmeticException + testIntFloorDivMod(4, 3, 1, 1); + testIntFloorDivMod(3, 3, 1, 0); + testIntFloorDivMod(2, 3, 0, 2); + testIntFloorDivMod(1, 3, 0, 1); + testIntFloorDivMod(0, 3, 0, 0); + testIntFloorDivMod(4, -3, -2, -2); + testIntFloorDivMod(3, -3, -1, 0); + testIntFloorDivMod(2, -3, -1, -1); + testIntFloorDivMod(1, -3, -1, -2); + testIntFloorDivMod(0, -3, 0, 0); + testIntFloorDivMod(-1, 3, -1, 2); + testIntFloorDivMod(-2, 3, -1, 1); + testIntFloorDivMod(-3, 3, -1, 0); + testIntFloorDivMod(-4, 3, -2, 2); + testIntFloorDivMod(-1, -3, 0, -1); + testIntFloorDivMod(-2, -3, 0, -2); + testIntFloorDivMod(-3, -3, 1, 0); + testIntFloorDivMod(-4, -3, 1, -1); + testIntFloorDivMod(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); + testIntFloorDivMod(Integer.MAX_VALUE, -1, -Integer.MAX_VALUE, 0); + testIntFloorDivMod(Integer.MAX_VALUE, 3, 715827882, 1); + testIntFloorDivMod(Integer.MAX_VALUE - 1, 3, 715827882, 0); + testIntFloorDivMod(Integer.MIN_VALUE, 3, -715827883, 1); + testIntFloorDivMod(Integer.MIN_VALUE + 1, 3, -715827883, 2); + testIntFloorDivMod(Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE, 0); + // Special case of integer overflow + testIntFloorDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0); + } + + /** + * Test FloorDiv and then FloorMod with int data. + */ + static void testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) { + testIntFloorDiv(x, y, divExpected); + testIntFloorMod(x, y, modExpected); + } + + /** + * Test FloorDiv with int data. + */ + static void testIntFloorDiv(int x, int y, Object expected) { + Object result = doFloorDiv(x, y); + if (!resultEquals(result, expected)) { + fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected); + } + + Object strict_result = doStrictFloorDiv(x, y); + if (!resultEquals(strict_result, expected)) { + fail("FAIL: StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected); + } + } + + /** + * Test FloorMod with int data. + */ + static void testIntFloorMod(int x, int y, Object expected) { + Object result = doFloorMod(x, y); + if (!resultEquals(result, expected)) { + fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected); + } + + Object strict_result = doStrictFloorMod(x, y); + if (!resultEquals(strict_result, expected)) { + fail("FAIL: StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected); + } + + try { + // Verify result against double precision floor function + int tmp = x / y; // Force ArithmeticException for divide by zero + double ff = x - Math.floor((double)x / (double)y) * y; + int fr = (int)ff; + if (fr != result) { + fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr); + } + } catch (ArithmeticException ae) { + if (y != 0) { + fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae); + } + } + } + + /** + * Test the floorDiv and floorMod methods for primitive long. + */ + static void testLongFloorDivMod() { + testLongFloorDivMod(4L, 0L, new ArithmeticException("/ by zero"), new ArithmeticException("/ by zero")); // Should throw ArithmeticException + testLongFloorDivMod(4L, 3L, 1L, 1L); + testLongFloorDivMod(3L, 3L, 1L, 0L); + testLongFloorDivMod(2L, 3L, 0L, 2L); + testLongFloorDivMod(1L, 3L, 0L, 1L); + testLongFloorDivMod(0L, 3L, 0L, 0L); + testLongFloorDivMod(4L, -3L, -2L, -2L); + testLongFloorDivMod(3L, -3L, -1L, 0l); + testLongFloorDivMod(2L, -3L, -1L, -1L); + testLongFloorDivMod(1L, -3L, -1L, -2L); + testLongFloorDivMod(0L, -3L, 0L, 0L); + testLongFloorDivMod(-1L, 3L, -1L, 2L); + testLongFloorDivMod(-2L, 3L, -1L, 1L); + testLongFloorDivMod(-3L, 3L, -1L, 0L); + testLongFloorDivMod(-4L, 3L, -2L, 2L); + testLongFloorDivMod(-1L, -3L, 0L, -1L); + testLongFloorDivMod(-2L, -3L, 0L, -2L); + testLongFloorDivMod(-3L, -3L, 1L, 0L); + testLongFloorDivMod(-4L, -3L, 1L, -1L); + + testLongFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L); + testLongFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L); + testLongFloorDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L, 1L); + testLongFloorDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L); + testLongFloorDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L - 1L, 1L); + testLongFloorDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L - 1L, 2L); + testLongFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L); + // Special case of integer overflow + testLongFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L); + } + + /** + * Test the integer floorDiv and floorMod methods. + * Math and StrictMath are tested and the same results are expected for both. + */ + static void testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) { + testLongFloorDiv(x, y, divExpected); + testLongFloorMod(x, y, modExpected); + } + + /** + * Test FloorDiv with long arguments against expected value. + * The expected value is usually a Long but in some cases is + * an ArithmeticException. + * + * @param x dividend + * @param y modulus + * @param expected expected value, + */ + static void testLongFloorDiv(long x, long y, Object expected) { + Object result = doFloorDiv(x, y); + if (!resultEquals(result, expected)) { + fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected); + } + + Object strict_result = doStrictFloorDiv(x, y); + if (!resultEquals(strict_result, expected)) { + fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected); + } + } + + /** + * Test FloorMod of long arguments against expected value. + * The expected value is usually a Long but in some cases is + * an ArithmeticException. + * + * @param x dividend + * @param y modulus + * @param expected expected value + */ + static void testLongFloorMod(long x, long y, Object expected) { + Object result = doFloorMod(x, y); + if (!resultEquals(result, expected)) { + fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected); + } + + Object strict_result = doStrictFloorMod(x, y); + if (!resultEquals(strict_result, expected)) { + fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected); + } + + try { + // Verify the result against BigDecimal rounding mode. + BigDecimal xD = new BigDecimal(x); + BigDecimal yD = new BigDecimal(y); + BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR); + resultD = resultD.multiply(yD); + resultD = xD.subtract(resultD); + long fr = resultD.longValue(); + if (fr != result) { + fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n",x, y, result, fr); + + } + } catch (ArithmeticException ae) { + if (y != 0) { + fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal"); + } + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doFloorDiv(int x, int y) { + try { + return Math.floorDiv(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doFloorDiv(long x, long y) { + try { + return Math.floorDiv(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doFloorMod(int x, int y) { + try { + return Math.floorMod(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doFloorMod(long x, long y) { + try { + return Math.floorMod(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doStrictFloorDiv(int x, int y) { + try { + return StrictMath.floorDiv(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doStrictFloorDiv(long x, long y) { + try { + return StrictMath.floorDiv(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doStrictFloorMod(int x, int y) { + try { + return StrictMath.floorMod(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Invoke floorDiv and return the result or any exception. + * @param x the x value + * @param y the y value + * @return the result Integer or an exception. + */ + static Object doStrictFloorMod(long x, long y) { + try { + return StrictMath.floorMod(x, y); + } catch (ArithmeticException ae) { + return ae; + } + } + + /** + * Returns a boolean by comparing the result and the expected value. + * The equals method is not defined for ArithmeticException but it is + * desirable to have equals return true if the expected and the result + * both threw the same exception (class and message.) + * + * @param result the result from testing the method + * @param expected the expected value + * @return true if the result is equal to the expected values; false otherwise. + */ + static boolean resultEquals(Object result, Object expected) { + if (result.getClass() != expected.getClass()) { + fail("FAIL: Result type mismatch, %s; expected: %s%n", + result.getClass().getName(), expected.getClass().getName()); + return false; + } + + if (result.equals(expected)) { + return true; + } + // Handle special case to compare ArithmeticExceptions + if (result instanceof ArithmeticException && expected instanceof ArithmeticException) { + ArithmeticException ae1 = (ArithmeticException)result; + ArithmeticException ae2 = (ArithmeticException)expected; + return ae1.getMessage().equals(ae2.getMessage()); + } + return false; + } + +}