8271602: Add Math.ceilDiv() family parallel to Math.floorDiv() family

Reviewed-by: bpb
This commit is contained in:
Raffaello Giulietti 2021-09-22 16:16:14 +00:00 committed by Brian Burkhalter
parent d39aad9230
commit da38ced329
4 changed files with 1093 additions and 53 deletions

View file

@ -967,6 +967,66 @@ public final class StrictMath {
return Math.floorDivExact(x, y);
}
/**
* Returns the smallest (closest to negative infinity)
* {@code int} value that is greater than or equal to the algebraic quotient.
* This method is identical to {@link #ceilDiv(int,int)} except that it
* throws an {@code ArithmeticException} when the dividend is
* {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is
* {@code -1} instead of ignoring the integer overflow and returning
* {@code Integer.MIN_VALUE}.
* <p>
* The ceil modulus method {@link #ceilMod(int,int)} is a suitable
* counterpart both for this method and for the {@link #ceilDiv(int,int)}
* method.
* <p>
* See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
* @return the smallest (closest to negative infinity)
* {@code int} value that is greater than or equal to the algebraic quotient.
* @throws ArithmeticException if the divisor {@code y} is zero, or the
* dividend {@code x} is {@code Integer.MIN_VALUE} and the divisor {@code y}
* is {@code -1}.
* @see Math#ceilDiv(int, int)
* @since 18
*/
public static int ceilDivExact(int x, int y) {
return Math.ceilDivExact(x, y);
}
/**
* Returns the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* This method is identical to {@link #ceilDiv(long,long)} except that it
* throws an {@code ArithmeticException} when the dividend is
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is
* {@code -1} instead of ignoring the integer overflow and returning
* {@code Long.MIN_VALUE}.
* <p>
* The ceil modulus method {@link #ceilMod(long,long)} is a suitable
* counterpart both for this method and for the {@link #ceilDiv(long,long)}
* method.
* <p>
* For examples, see {@link Math#ceilDiv(int, int) Math.ceilDiv}.
*
* @param x the dividend
* @param y the divisor
* @return the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* @throws ArithmeticException if the divisor {@code y} is zero, or the
* dividend {@code x} is {@code Long.MIN_VALUE} and the divisor {@code y}
* is {@code -1}.
* @see Math#ceilDiv(int, int)
* @see Math#ceilDiv(long,long)
* @since 18
*/
public static long ceilDivExact(long x, long y) {
return Math.ceilDivExact(x, y);
}
/**
* Returns the argument incremented by one,
* throwing an exception if the result overflows an {@code int}.
@ -1270,6 +1330,162 @@ public final class StrictMath {
return Math.floorMod(x, y);
}
/**
* Returns the smallest (closest to negative infinity)
* {@code int} value that is greater than or equal to the algebraic quotient.
* There is one special case: if the dividend is
* {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to {@code Integer.MIN_VALUE}.
* <p>
* See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
* @return the smallest (closest to negative infinity)
* {@code int} value that is greater than or equal to the algebraic quotient.
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilDiv(int, int)
* @see Math#ceil(double)
* @since 18
*/
public static int ceilDiv(int x, int y) {
return Math.ceilDiv(x, y);
}
/**
* Returns the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* There is one special case: if the dividend is
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to {@code Long.MIN_VALUE}.
* <p>
* See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
* @return the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilDiv(long, int)
* @see Math#ceil(double)
* @since 18
*/
public static long ceilDiv(long x, int y) {
return Math.ceilDiv(x, y);
}
/**
* Returns the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* There is one special case: if the dividend is
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to {@code Long.MIN_VALUE}.
* <p>
* See {@link Math#ceilDiv(int, int) Math.ceilDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
* @return the smallest (closest to negative infinity)
* {@code long} value that is greater than or equal to the algebraic quotient.
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilDiv(long, long)
* @see Math#ceil(double)
* @since 18
*/
public static long ceilDiv(long x, long y) {
return Math.ceilDiv(x, y);
}
/**
* Returns the ceiling modulus of the {@code int} arguments.
* <p>
* The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)},
* has the opposite sign as the divisor {@code y} or is zero, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
*
* <p>
* The relationship between {@code ceilDiv} and {@code ceilMod} is such that:
* <ul>
* <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li>
* </ul>
* <p>
* See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
* @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)}
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilMod(int, int)
* @see StrictMath#ceilDiv(int, int)
* @since 18
*/
public static int ceilMod(int x, int y) {
return Math.ceilMod(x , y);
}
/**
* Returns the ceiling modulus of the {@code long} and {@code int} arguments.
* <p>
* The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)},
* has the opposite sign as the divisor {@code y} or is zero, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
*
* <p>
* The relationship between {@code ceilDiv} and {@code ceilMod} is such that:
* <ul>
* <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li>
* </ul>
* <p>
* See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
* @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)}
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilMod(long, int)
* @see StrictMath#ceilDiv(long, int)
* @since 18
*/
public static int ceilMod(long x, int y) {
return Math.ceilMod(x , y);
}
/**
* Returns the ceiling modulus of the {@code long} arguments.
* <p>
* The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)},
* has the opposite sign as the divisor {@code y} or is zero, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
*
* <p>
* The relationship between {@code ceilDiv} and {@code ceilMod} is such that:
* <ul>
* <li>{@code ceilDiv(x, y) * y + ceilMod(x, y) == x}</li>
* </ul>
* <p>
* See {@link Math#ceilMod(int, int) Math.ceilMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
* @return the ceiling modulus {@code x - (ceilDiv(x, y) * y)}
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#ceilMod(long, long)
* @see StrictMath#ceilDiv(long, long)
* @since 18
*/
public static long ceilMod(long x, long y) {
return Math.ceilMod(x, y);
}
/**
* Returns the absolute value of an {@code int} value.
* If the argument is not negative, the argument is returned.