In Java, Math.ceilDivExact(x, y) divides two whole numbers and rounds the quotient up, toward positive infinity. Math.ceilDivExact(23, 10) is 3, because 23 items need 3 boxes of 10.
It is the overflow-checked sibling of ceilDiv. Only one input can overflow, MIN_VALUE divided by -1; there ceilDivExact throws an ArithmeticException instead of returning a wrong number.
A static method of the java.lang.Math class, added in Java 18 (JDK 18). Call it on the class name, as in Math.ceilDivExact(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.
Syntax
Pass the total first and the group size second, both int or both long. The result is the smallest whole number of groups that holds everything.
Java
int boxes = Math.ceilDivExact(23, 10); // 3
long pages = Math.ceilDivExact(1_000_000_001L, 10L); // 100000001
Math.ceilDivExact(5, 0); // throws ArithmeticException
Call
What it does
Math.ceilDivExact(23, 10)
int overload: 2.3 rounds up to 3.
Math.ceilDivExact(20, 10)
Already exact: returns 2.
Math.ceilDivExact(-23, 10)
Up means toward positive infinity: -2.3 becomes -2.
Math.ceilDivExact(Integer.MIN_VALUE, -1)
Throws ArithmeticException ("integer overflow").
The classic trick (items + perBox - 1) / perBox gives the same answer for small positive numbers, but the addition can itself overflow for large ones. ceilDivExact has no such problem and also works for negative numbers.
Library declarations
Java
// java.lang.Math (Java 18+)
public static int ceilDivExact(int x, int y)
public static long ceilDivExact(long x, long y)
Parameters
Parameter
Description
x
The dividend (for example the number of items).
y
The divisor (for example the capacity of one box). Must not be zero.
Return Value
Return Value
Description
int / long
The quotient rounded up: the smallest whole number that is greater than or equal to the exact quotient.
Throws ArithmeticException
/ by zero if y is 0; integer overflow or long overflow for MIN_VALUE / -1.
Exactly full vs one over. 96 items fill exactly 2 boxes of 48, while 97 items need a third box; 0 items need none, and 130 divided by 48 is about 2.7, so it also rounds up to 3.
The trick overflows.Integer.MAX_VALUE + 9 wraps to -2147483640, and dividing that by 10 gives -214748364, a negative number of boxes.
ceilDivExact is correct. 2147483647 divided by 10 is 214748364.7, which rounds up to 214748365; no intermediate addition is needed, so nothing can overflow.
Example 3 — Zero Divisor and MIN_VALUE / -1
The inputs that make ceilDivExact throw, compared with plain ceilDiv. Java 18+.
plain ceilDiv: -2147483648
ceilDivExact: integer overflow
long version: long overflow
zero divisor: / by zero
How It Works
The wrap of the plain version. The true quotient of -2147483648 and -1 is 2147483648, which does not fit, so Math.ceilDiv returns -2147483648 without any warning.
The exact version objects.ceilDivExact throws integer overflow, and the long pair with Long.MIN_VALUE throws long overflow.
Division by zero. A divisor of 0 is rejected with the usual / by zero message before any rounding is attempted.
Frequently Asked Questions
It divides two ints or two longs and rounds the quotient up toward positive infinity. It throws ArithmeticException for a zero divisor and for the overflow case MIN_VALUE / -1.
Java 18, along with ceilDiv, ceilMod, floorDivExact, and divideExact. On older versions use (a + b - 1) / b for small positive values.
Use Math.ceilDivExact(items, perBox) or Math.ceilDiv(items, perBox). It rounds up, so 97 items in boxes of 48 gives 3.
They return the same value, except for Integer.MIN_VALUE / -1 and Long.MIN_VALUE / -1L. ceilDiv wraps to MIN_VALUE, while ceilDivExact throws ArithmeticException.
It rounds toward positive infinity, so ceilDivExact(-23, 10) is -2 and ceilDivExact(23, -10) is -2 as well. Only results that are exact whole numbers stay unchanged.