In Java, Math.ceilMod(x, y) returns the remainder that matches Math.ceilDiv. It always satisfies ceilDiv(x, y) * y + ceilMod(x, y) == x. Because ceilDiv rounds up, the remainder is zero or has the opposite sign of the divisor.
For positive numbers that means ceilMod is zero or negative, and its absolute value tells you how many items are missing to fill the last box: Math.ceilMod(23, 10) is -7, because 3 boxes of 10 hold 30 items and you have only 23.
A static method of the java.lang.Math class, added in Java 18 (JDK 18). Call it on the class name, as in Math.ceilMod(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.
Syntax
Pass the dividend, then the divisor. The result is never larger in size than the divisor.
Java
int r1 = Math.ceilMod(23, 10); // -7
int r2 = Math.ceilMod(30, 10); // 0
int r3 = Math.ceilMod(-17, 5); // -2
int r4 = Math.ceilMod(10_000_000_000L, 3); // -2 (an int)
Call
What it does
Math.ceilMod(23, 10)
3 boxes of 10 minus 23 items: returns -7.
-Math.ceilMod(23, 10)
Items short of a full box: 7.
Math.ceilMod(7, -2)
Negative divisor, so the remainder is positive: returns 1.
Math.ceilMod(10_000_000_000L, 3)
long dividend, int divisor: the result is an int.
With a long dividend and an int divisor, the remainder always fits in an int, so ceilMod(long, int) returns an int. With two long arguments it returns a long.
Library declarations
Java
// java.lang.Math (Java 18+)
public static int ceilMod(int x, int y)
public static int ceilMod(long x, int y)
public static long ceilMod(long x, long y)
Parameters
Parameter
Description
x
The dividend (an int or a long).
y
The divisor (an int or a long). Must not be zero.
Return Value
Return Value
Description
int / long
x - ceilDiv(x, y) * y: zero, or a value with the opposite sign of y. The (long, int) overload returns an int.
The % column follows the dividend. 7 gives +1 and -7 gives -1, whatever the divisor is.
The floorMod column follows the divisor. With divisor 2 the results are +1 for both 7 and -7; with divisor -2 they are -1.
The ceilMod column does the opposite. For divisor 2 the results are -1 (the distance up to the next even number), and for divisor -2 they are +1, so ceilMod always points from x up to the next multiple.
Example 3 — The ceilDiv/ceilMod Identity, the long Overload, and Zero
Rebuild the dividend from its quotient and remainder, then try the long input and a zero divisor. Java 18+.
Java
public class MathCeilModIdentity {
public static void main(String[] args) {
int x = -17;
int y = 5;
int q = Math.ceilDiv(x, y);
int r = Math.ceilMod(x, y);
System.out.println("ceilDiv = " + q);
System.out.println("ceilMod = " + r);
System.out.println("q * y + r == x? " + (q * y + r == x));
int small = Math.ceilMod(10_000_000_000L, 3);
System.out.println("long, int: " + small);
try {
Math.ceilMod(5, 0);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output
ceilDiv = -3
ceilMod = -2
q * y + r == x? true
long, int: -2
Error: / by zero
How It Works
Rounding toward zero here. -17 / 5 is -3.4; rounding up gives -3, and the remainder that completes the sum is -17 - (-3 * 5) = -2.
The identity holds. -3 * 5 + (-2) is -17, so quotient and remainder always rebuild the original number.
An int from a long. 10000000000 rounds up to 3333333334 times 3, which is 10000000002, so the remainder is -2 and fits in an int; a zero divisor throws just like ceilDiv.
Frequently Asked Questions
It returns the remainder that pairs with Math.ceilDiv: x - ceilDiv(x, y) * y. The result is zero or has the opposite sign of the divisor, so for positive numbers it is zero or negative.
Java 18, together with ceilDiv and the Exact variants. On older versions, compute it yourself from a rounded-up quotient q as x - q * y.
Because ceilDiv(23, 10) is 3, and 3 * 10 is 30, which is 7 more than 23. The remainder is 23 - 30 = -7, and its absolute value is the number of items missing from the last box.
The % operator takes the sign of the dividend, floorMod takes the sign of the divisor, and ceilMod takes the opposite sign of the divisor (or is zero). Each one pairs with its own division: plain /, floorDiv, and ceilDiv.
An int. The remainder is always smaller in size than the int divisor, so it fits in an int. The (long, long) overload returns a long.