src/java.base/share/classes/java/lang/Math.java

Print this page
rev 12809 : 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
Summary: Add new methods with long, int signatures.
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  77  * method at different arguments is also important.  Therefore, most
  78  * methods with more than 0.5 ulp errors are required to be
  79  * <i>semi-monotonic</i>: whenever the mathematical function is
  80  * non-decreasing, so is the floating-point approximation, likewise,
  81  * whenever the mathematical function is non-increasing, so is the
  82  * floating-point approximation.  Not all approximations that have 1
  83  * ulp accuracy will automatically meet the monotonicity requirements.
  84  *
  85  * <p>
  86  * The platform uses signed two's complement integer arithmetic with
  87  * int and long primitive types.  The developer should choose
  88  * the primitive type to ensure that arithmetic operations consistently
  89  * produce correct results, which in some cases means the operations
  90  * will not overflow the range of values of the computation.
  91  * The best practice is to choose the primitive type and algorithm to avoid
  92  * overflow. In cases where the size is {@code int} or {@code long} and
  93  * overflow errors need to be detected, the methods {@code addExact},
  94  * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
  95  * throw an {@code ArithmeticException} when the results overflow.
  96  * For other arithmetic operations such as divide, absolute value,
  97  * increment, decrement, and negation overflow occurs only with
  98  * a specific minimum or maximum value and should be checked against
  99  * the minimum or maximum as appropriate.
 100  *
 101  * @author  unascribed
 102  * @author  Joseph D. Darcy
 103  * @since   1.0
 104  */
 105 
 106 public final class Math {
 107 
 108     /**
 109      * Don't let anyone instantiate this class.
 110      */
 111     private Math() {}
 112 
 113     /**
 114      * The {@code double} value that is closer than any other to
 115      * <i>e</i>, the base of the natural logarithms.
 116      */
 117     public static final double E = 2.7182818284590452354;


 843         if (((x ^ r) & (y ^ r)) < 0) {
 844             throw new ArithmeticException("long overflow");
 845         }
 846         return r;
 847     }
 848 
 849     /**
 850      * Returns the difference of the arguments,
 851      * throwing an exception if the result overflows an {@code int}.
 852      *
 853      * @param x the first value
 854      * @param y the second value to subtract from the first
 855      * @return the result
 856      * @throws ArithmeticException if the result overflows an int
 857      * @since 1.8
 858      */
 859     @HotSpotIntrinsicCandidate
 860     public static int subtractExact(int x, int y) {
 861         int r = x - y;
 862         // HD 2-12 Overflow iff the arguments have different signs and
 863         // the sign of the result is different than the sign of x
 864         if (((x ^ y) & (x ^ r)) < 0) {
 865             throw new ArithmeticException("integer overflow");
 866         }
 867         return r;
 868     }
 869 
 870     /**
 871      * Returns the difference of the arguments,
 872      * throwing an exception if the result overflows a {@code long}.
 873      *
 874      * @param x the first value
 875      * @param y the second value to subtract from the first
 876      * @return the result
 877      * @throws ArithmeticException if the result overflows a long
 878      * @since 1.8
 879      */
 880     @HotSpotIntrinsicCandidate
 881     public static long subtractExact(long x, long y) {
 882         long r = x - y;
 883         // HD 2-12 Overflow iff the arguments have different signs and
 884         // the sign of the result is different than the sign of x
 885         if (((x ^ y) & (x ^ r)) < 0) {
 886             throw new ArithmeticException("long overflow");
 887         }
 888         return r;
 889     }
 890 
 891     /**
 892      * Returns the product of the arguments,
 893      * throwing an exception if the result overflows an {@code int}.
 894      *
 895      * @param x the first value
 896      * @param y the second value
 897      * @return the result
 898      * @throws ArithmeticException if the result overflows an int
 899      * @since 1.8
 900      */
 901     @HotSpotIntrinsicCandidate
 902     public static int multiplyExact(int x, int y) {
 903         long r = (long)x * (long)y;
 904         if ((int)r != r) {
 905             throw new ArithmeticException("integer overflow");
 906         }
 907         return (int)r;
 908     }
 909 
 910     /**














 911      * Returns the product of the arguments,
 912      * throwing an exception if the result overflows a {@code long}.
 913      *
 914      * @param x the first value
 915      * @param y the second value
 916      * @return the result
 917      * @throws ArithmeticException if the result overflows a long
 918      * @since 1.8
 919      */
 920     @HotSpotIntrinsicCandidate
 921     public static long multiplyExact(long x, long y) {
 922         long r = x * y;
 923         long ax = Math.abs(x);
 924         long ay = Math.abs(y);
 925         if (((ax | ay) >>> 31 != 0)) {
 926             // Some bits greater than 2^31 that might cause overflow
 927             // Check the result using the divide operator
 928             // and check for the special case of Long.MIN_VALUE * -1
 929            if (((y != 0) && (r / y != x)) ||
 930                (x == Long.MIN_VALUE && y == -1)) {


1047      * throwing an exception if the value overflows an {@code int}.
1048      *
1049      * @param value the long value
1050      * @return the argument as an int
1051      * @throws ArithmeticException if the {@code argument} overflows an int
1052      * @since 1.8
1053      */
1054     public static int toIntExact(long value) {
1055         if ((int)value != value) {
1056             throw new ArithmeticException("integer overflow");
1057         }
1058         return (int)value;
1059     }
1060 
1061     /**
1062      * Returns the largest (closest to positive infinity)
1063      * {@code int} value that is less than or equal to the algebraic quotient.
1064      * There is one special case, if the dividend is the
1065      * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
1066      * then integer overflow occurs and
1067      * the result is equal to the {@code Integer.MIN_VALUE}.
1068      * <p>
1069      * Normal integer division operates under the round to zero rounding mode
1070      * (truncation).  This operation instead acts under the round toward
1071      * negative infinity (floor) rounding mode.
1072      * The floor rounding mode gives different results than truncation
1073      * when the exact result is negative.
1074      * <ul>
1075      *   <li>If the signs of the arguments are the same, the results of
1076      *       {@code floorDiv} and the {@code /} operator are the same.  <br>
1077      *       For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1078      *   <li>If the signs of the arguments are different,  the quotient is negative and
1079      *       {@code floorDiv} returns the integer less than or equal to the quotient
1080      *       and the {@code /} operator returns the integer closest to zero.<br>
1081      *       For example, {@code floorDiv(-4, 3) == -2},
1082      *       whereas {@code (-4 / 3) == -1}.
1083      *   </li>
1084      * </ul>
1085      *
1086      * @param x the dividend
1087      * @param y the divisor
1088      * @return the largest (closest to positive infinity)
1089      * {@code int} value that is less than or equal to the algebraic quotient.
1090      * @throws ArithmeticException if the divisor {@code y} is zero
1091      * @see #floorMod(int, int)
1092      * @see #floor(double)
1093      * @since 1.8
1094      */
1095     public static int floorDiv(int x, int y) {
1096         int r = x / y;
1097         // if the signs are different and modulo not zero, round down
1098         if ((x ^ y) < 0 && (r * y != x)) {
1099             r--;
1100         }
1101         return r;
1102     }
1103 
1104     /**
1105      * Returns the largest (closest to positive infinity)
1106      * {@code long} value that is less than or equal to the algebraic quotient.
1107      * There is one special case, if the dividend is the
1108      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
1109      * then integer overflow occurs and
1110      * the result is equal to the {@code Long.MIN_VALUE}.





























1111      * <p>
1112      * Normal integer division operates under the round to zero rounding mode
1113      * (truncation).  This operation instead acts under the round toward
1114      * negative infinity (floor) rounding mode.
1115      * The floor rounding mode gives different results than truncation
1116      * when the exact result is negative.
1117      * <p>
1118      * For examples, see {@link #floorDiv(int, int)}.
1119      *
1120      * @param x the dividend
1121      * @param y the divisor
1122      * @return the largest (closest to positive infinity)
1123      * {@code long} value that is less than or equal to the algebraic quotient.
1124      * @throws ArithmeticException if the divisor {@code y} is zero
1125      * @see #floorMod(long, long)
1126      * @see #floor(double)
1127      * @since 1.8
1128      */
1129     public static long floorDiv(long x, long y) {
1130         long r = x / y;
1131         // if the signs are different and modulo not zero, round down
1132         if ((x ^ y) < 0 && (r * y != x)) {
1133             r--;
1134         }
1135         return r;


1163      *   <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br>
1164      *      <ul>
1165      *      <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1} </li>
1166      *      <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1} </li>
1167      *      <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1 } </li>
1168      *      </ul>
1169      *   </li>
1170      * </ul>
1171      * <p>
1172      * If the signs of arguments are unknown and a positive modulus
1173      * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
1174      *
1175      * @param x the dividend
1176      * @param y the divisor
1177      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1178      * @throws ArithmeticException if the divisor {@code y} is zero
1179      * @see #floorDiv(int, int)
1180      * @since 1.8
1181      */
1182     public static int floorMod(int x, int y) {
1183         int r = x - floorDiv(x, y) * y;
1184         return r;































1185     }
1186 
1187     /**
1188      * Returns the floor modulus of the {@code long} arguments.
1189      * <p>
1190      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1191      * has the same sign as the divisor {@code y}, and
1192      * is in the range of {@code -abs(y) < r < +abs(y)}.
1193      *
1194      * <p>
1195      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1196      * <ul>
1197      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1198      * </ul>
1199      * <p>
1200      * For examples, see {@link #floorMod(int, int)}.
1201      *
1202      * @param x the dividend
1203      * @param y the divisor
1204      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}


   1 /*
   2  * Copyright (c) 1994, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  77  * method at different arguments is also important.  Therefore, most
  78  * methods with more than 0.5 ulp errors are required to be
  79  * <i>semi-monotonic</i>: whenever the mathematical function is
  80  * non-decreasing, so is the floating-point approximation, likewise,
  81  * whenever the mathematical function is non-increasing, so is the
  82  * floating-point approximation.  Not all approximations that have 1
  83  * ulp accuracy will automatically meet the monotonicity requirements.
  84  *
  85  * <p>
  86  * The platform uses signed two's complement integer arithmetic with
  87  * int and long primitive types.  The developer should choose
  88  * the primitive type to ensure that arithmetic operations consistently
  89  * produce correct results, which in some cases means the operations
  90  * will not overflow the range of values of the computation.
  91  * The best practice is to choose the primitive type and algorithm to avoid
  92  * overflow. In cases where the size is {@code int} or {@code long} and
  93  * overflow errors need to be detected, the methods {@code addExact},
  94  * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
  95  * throw an {@code ArithmeticException} when the results overflow.
  96  * For other arithmetic operations such as divide, absolute value,
  97  * increment by one, decrement by one, and negation, overflow occurs only with
  98  * a specific minimum or maximum value and should be checked against
  99  * the minimum or maximum as appropriate.
 100  *
 101  * @author  unascribed
 102  * @author  Joseph D. Darcy
 103  * @since   1.0
 104  */
 105 
 106 public final class Math {
 107 
 108     /**
 109      * Don't let anyone instantiate this class.
 110      */
 111     private Math() {}
 112 
 113     /**
 114      * The {@code double} value that is closer than any other to
 115      * <i>e</i>, the base of the natural logarithms.
 116      */
 117     public static final double E = 2.7182818284590452354;


 843         if (((x ^ r) & (y ^ r)) < 0) {
 844             throw new ArithmeticException("long overflow");
 845         }
 846         return r;
 847     }
 848 
 849     /**
 850      * Returns the difference of the arguments,
 851      * throwing an exception if the result overflows an {@code int}.
 852      *
 853      * @param x the first value
 854      * @param y the second value to subtract from the first
 855      * @return the result
 856      * @throws ArithmeticException if the result overflows an int
 857      * @since 1.8
 858      */
 859     @HotSpotIntrinsicCandidate
 860     public static int subtractExact(int x, int y) {
 861         int r = x - y;
 862         // HD 2-12 Overflow iff the arguments have different signs and
 863         // the sign of the result is different from the sign of x
 864         if (((x ^ y) & (x ^ r)) < 0) {
 865             throw new ArithmeticException("integer overflow");
 866         }
 867         return r;
 868     }
 869 
 870     /**
 871      * Returns the difference of the arguments,
 872      * throwing an exception if the result overflows a {@code long}.
 873      *
 874      * @param x the first value
 875      * @param y the second value to subtract from the first
 876      * @return the result
 877      * @throws ArithmeticException if the result overflows a long
 878      * @since 1.8
 879      */
 880     @HotSpotIntrinsicCandidate
 881     public static long subtractExact(long x, long y) {
 882         long r = x - y;
 883         // HD 2-12 Overflow iff the arguments have different signs and
 884         // the sign of the result is different from the sign of x
 885         if (((x ^ y) & (x ^ r)) < 0) {
 886             throw new ArithmeticException("long overflow");
 887         }
 888         return r;
 889     }
 890 
 891     /**
 892      * Returns the product of the arguments,
 893      * throwing an exception if the result overflows an {@code int}.
 894      *
 895      * @param x the first value
 896      * @param y the second value
 897      * @return the result
 898      * @throws ArithmeticException if the result overflows an int
 899      * @since 1.8
 900      */
 901     @HotSpotIntrinsicCandidate
 902     public static int multiplyExact(int x, int y) {
 903         long r = (long)x * (long)y;
 904         if ((int)r != r) {
 905             throw new ArithmeticException("integer overflow");
 906         }
 907         return (int)r;
 908     }
 909 
 910     /**
 911      * Returns the product of the arguments, throwing an exception if the result
 912      * overflows a {@code long}.
 913      *
 914      * @param x the first value
 915      * @param y the second value
 916      * @return the result
 917      * @throws ArithmeticException if the result overflows a long
 918      * @since 1.9
 919      */
 920     public static long multiplyExact(long x, int y) {
 921         return multiplyExact(x, (long)y);
 922     }
 923 
 924     /**
 925      * Returns the product of the arguments,
 926      * throwing an exception if the result overflows a {@code long}.
 927      *
 928      * @param x the first value
 929      * @param y the second value
 930      * @return the result
 931      * @throws ArithmeticException if the result overflows a long
 932      * @since 1.8
 933      */
 934     @HotSpotIntrinsicCandidate
 935     public static long multiplyExact(long x, long y) {
 936         long r = x * y;
 937         long ax = Math.abs(x);
 938         long ay = Math.abs(y);
 939         if (((ax | ay) >>> 31 != 0)) {
 940             // Some bits greater than 2^31 that might cause overflow
 941             // Check the result using the divide operator
 942             // and check for the special case of Long.MIN_VALUE * -1
 943            if (((y != 0) && (r / y != x)) ||
 944                (x == Long.MIN_VALUE && y == -1)) {


1061      * throwing an exception if the value overflows an {@code int}.
1062      *
1063      * @param value the long value
1064      * @return the argument as an int
1065      * @throws ArithmeticException if the {@code argument} overflows an int
1066      * @since 1.8
1067      */
1068     public static int toIntExact(long value) {
1069         if ((int)value != value) {
1070             throw new ArithmeticException("integer overflow");
1071         }
1072         return (int)value;
1073     }
1074 
1075     /**
1076      * Returns the largest (closest to positive infinity)
1077      * {@code int} value that is less than or equal to the algebraic quotient.
1078      * There is one special case, if the dividend is the
1079      * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
1080      * then integer overflow occurs and
1081      * the result is equal to {@code Integer.MIN_VALUE}.
1082      * <p>
1083      * Normal integer division operates under the round to zero rounding mode
1084      * (truncation).  This operation instead acts under the round toward
1085      * negative infinity (floor) rounding mode.
1086      * The floor rounding mode gives different results from truncation
1087      * when the exact result is negative.
1088      * <ul>
1089      *   <li>If the signs of the arguments are the same, the results of
1090      *       {@code floorDiv} and the {@code /} operator are the same.  <br>
1091      *       For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1092      *   <li>If the signs of the arguments are different,  the quotient is negative and
1093      *       {@code floorDiv} returns the integer less than or equal to the quotient
1094      *       and the {@code /} operator returns the integer closest to zero.<br>
1095      *       For example, {@code floorDiv(-4, 3) == -2},
1096      *       whereas {@code (-4 / 3) == -1}.
1097      *   </li>
1098      * </ul>
1099      *
1100      * @param x the dividend
1101      * @param y the divisor
1102      * @return the largest (closest to positive infinity)
1103      * {@code int} value that is less than or equal to the algebraic quotient.
1104      * @throws ArithmeticException if the divisor {@code y} is zero
1105      * @see #floorMod(int, int)
1106      * @see #floor(double)
1107      * @since 1.8
1108      */
1109     public static int floorDiv(int x, int y) {
1110         int r = x / y;
1111         // if the signs are different and modulo not zero, round down
1112         if ((x ^ y) < 0 && (r * y != x)) {
1113             r--;
1114         }
1115         return r;
1116     }
1117 
1118     /**
1119      * Returns the largest (closest to positive infinity)
1120      * {@code long} value that is less than or equal to the algebraic quotient.
1121      * There is one special case, if the dividend is the
1122      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
1123      * then integer overflow occurs and
1124      * the result is equal to {@code Long.MIN_VALUE}.
1125      * <p>
1126      * Normal integer division operates under the round to zero rounding mode
1127      * (truncation).  This operation instead acts under the round toward
1128      * negative infinity (floor) rounding mode.
1129      * The floor rounding mode gives different results from truncation
1130      * when the exact result is negative.
1131      * <p>
1132      * For examples, see {@link #floorDiv(int, int)}.
1133      *
1134      * @param x the dividend
1135      * @param y the divisor
1136      * @return the largest (closest to positive infinity)
1137      * {@code int} value that is less than or equal to the algebraic quotient.
1138      * @throws ArithmeticException if the divisor {@code y} is zero
1139      * @see #floorMod(long, int)
1140      * @see #floor(double)
1141      * @since 1.9
1142      */
1143     public static long floorDiv(long x, int y) {
1144         return floorDiv(x, (long)y);
1145     }
1146 
1147     /**
1148      * Returns the largest (closest to positive infinity)
1149      * {@code long} value that is less than or equal to the algebraic quotient.
1150      * There is one special case, if the dividend is the
1151      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
1152      * then integer overflow occurs and
1153      * the result is equal to {@code Long.MIN_VALUE}.
1154      * <p>
1155      * Normal integer division operates under the round to zero rounding mode
1156      * (truncation).  This operation instead acts under the round toward
1157      * negative infinity (floor) rounding mode.
1158      * The floor rounding mode gives different results from truncation
1159      * when the exact result is negative.
1160      * <p>
1161      * For examples, see {@link #floorDiv(int, int)}.
1162      *
1163      * @param x the dividend
1164      * @param y the divisor
1165      * @return the largest (closest to positive infinity)
1166      * {@code long} value that is less than or equal to the algebraic quotient.
1167      * @throws ArithmeticException if the divisor {@code y} is zero
1168      * @see #floorMod(long, long)
1169      * @see #floor(double)
1170      * @since 1.8
1171      */
1172     public static long floorDiv(long x, long y) {
1173         long r = x / y;
1174         // if the signs are different and modulo not zero, round down
1175         if ((x ^ y) < 0 && (r * y != x)) {
1176             r--;
1177         }
1178         return r;


1206      *   <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br>
1207      *      <ul>
1208      *      <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1} </li>
1209      *      <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1} </li>
1210      *      <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1 } </li>
1211      *      </ul>
1212      *   </li>
1213      * </ul>
1214      * <p>
1215      * If the signs of arguments are unknown and a positive modulus
1216      * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
1217      *
1218      * @param x the dividend
1219      * @param y the divisor
1220      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1221      * @throws ArithmeticException if the divisor {@code y} is zero
1222      * @see #floorDiv(int, int)
1223      * @since 1.8
1224      */
1225     public static int floorMod(int x, int y) {
1226         return x - floorDiv(x, y) * y;
1227     }
1228 
1229     /**
1230      * Returns the floor modulus of the {@code long} and {@int} arguments.
1231      * <p>
1232      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1233      * has the same sign as the divisor {@code y}, and
1234      * is in the range of {@code -abs(y) < r < +abs(y)}.
1235      *
1236      * <p>
1237      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1238      * <ul>
1239      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1240      * </ul>
1241      * <p>
1242      * For examples, see {@link #floorMod(int, int)}.
1243      *
1244      * @param x the dividend
1245      * @param y the divisor
1246      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1247      * @throws ArithmeticException if the divisor {@code y} is zero or the
1248      *         result would overflow an {@code int}
1249      * @see #floorDiv(long, int)
1250      * @since 1.9
1251      */
1252     public static int floorMod(long x, int y) {
1253         long r = x - floorDiv(x, y) * y;
1254         int result = (int)r;
1255         if (result != r) {
1256             throw new ArithmeticException("integer overlow");
1257         }
1258         return result;
1259     }
1260 
1261     /**
1262      * Returns the floor modulus of the {@code long} arguments.
1263      * <p>
1264      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1265      * has the same sign as the divisor {@code y}, and
1266      * is in the range of {@code -abs(y) < r < +abs(y)}.
1267      *
1268      * <p>
1269      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1270      * <ul>
1271      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1272      * </ul>
1273      * <p>
1274      * For examples, see {@link #floorMod(int, int)}.
1275      *
1276      * @param x the dividend
1277      * @param y the divisor
1278      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}