1020 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
1021 * then integer overflow occurs and
1022 * the result is equal to the {@code Integer.MIN_VALUE}.
1023 * <p>
1024 * Normal integer division operates under the round to zero rounding mode
1025 * (truncation). This operation instead acts under the round toward
1026 * negative infinity (floor) rounding mode.
1027 * The floor rounding mode gives different results than truncation
1028 * when the exact result is negative.
1029 * <ul>
1030 * <li>If the signs of the arguments are the same, the results of
1031 * {@code floorDiv} and the {@code /} operator are the same. <br>
1032 * For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1033 * <li>If the signs of the arguments are different, the quotient is negative and
1034 * {@code floorDiv} returns the integer less than or equal to the quotient
1035 * and the {@code /} operator returns the integer closest to zero.<br>
1036 * For example, {@code floorDiv(-4, 3) == -2},
1037 * whereas {@code (-4 / 3) == -1}.
1038 * </li>
1039 * </ul>
1040 * <p>
1041 *
1042 * @param x the dividend
1043 * @param y the divisor
1044 * @return the largest (closest to positive infinity)
1045 * {@code int} value that is less than or equal to the algebraic quotient.
1046 * @throws ArithmeticException if the divisor {@code y} is zero
1047 * @see #floorMod(int, int)
1048 * @see #floor(double)
1049 * @since 1.8
1050 */
1051 public static int floorDiv(int x, int y) {
1052 int r = x / y;
1053 // if the signs are different and modulo not zero, round down
1054 if ((x ^ y) < 0 && (r * y != x)) {
1055 r--;
1056 }
1057 return r;
1058 }
1059
1060 /**
|
1020 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
1021 * then integer overflow occurs and
1022 * the result is equal to the {@code Integer.MIN_VALUE}.
1023 * <p>
1024 * Normal integer division operates under the round to zero rounding mode
1025 * (truncation). This operation instead acts under the round toward
1026 * negative infinity (floor) rounding mode.
1027 * The floor rounding mode gives different results than truncation
1028 * when the exact result is negative.
1029 * <ul>
1030 * <li>If the signs of the arguments are the same, the results of
1031 * {@code floorDiv} and the {@code /} operator are the same. <br>
1032 * For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1033 * <li>If the signs of the arguments are different, the quotient is negative and
1034 * {@code floorDiv} returns the integer less than or equal to the quotient
1035 * and the {@code /} operator returns the integer closest to zero.<br>
1036 * For example, {@code floorDiv(-4, 3) == -2},
1037 * whereas {@code (-4 / 3) == -1}.
1038 * </li>
1039 * </ul>
1040 *
1041 * @param x the dividend
1042 * @param y the divisor
1043 * @return the largest (closest to positive infinity)
1044 * {@code int} value that is less than or equal to the algebraic quotient.
1045 * @throws ArithmeticException if the divisor {@code y} is zero
1046 * @see #floorMod(int, int)
1047 * @see #floor(double)
1048 * @since 1.8
1049 */
1050 public static int floorDiv(int x, int y) {
1051 int r = x / y;
1052 // if the signs are different and modulo not zero, round down
1053 if ((x ^ y) < 0 && (r * y != x)) {
1054 r--;
1055 }
1056 return r;
1057 }
1058
1059 /**
|