Java Math ceilMod() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 18+

Definition

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)
CallWhat 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

ParameterDescription
xThe dividend (an int or a long).
yThe divisor (an int or a long). Must not be zero.

Return Value

Return ValueDescription
int / longx - ceilDiv(x, y) * y: zero, or a value with the opposite sign of y. The (long, int) overload returns an int.
Throws ArithmeticExceptionWhen y is zero. The message is / by zero.

Compare with Similar Methods

The three kinds of remainder side by side:

MethodHow it differs
ceilDiv(x, y)The quotient that pairs with ceilMod (rounded up).
floorMod(x, y)Pairs with floorDiv; takes the sign of the divisor, so it is never negative for a positive y.
x % yTakes the sign of the dividend; pairs with plain / (rounding toward zero).
floorDiv(x, y)The rounded-down quotient used with floorMod.

Java Examples

Example 1 — How Many Items Are Missing From the Last Box?

Orders are packed 10 to a box. ceilDiv counts the boxes and ceilMod tells how far the last box is from full. Java 18+.

Java
public class MathCeilModBoxes {
    public static void main(String[] args) {
        int boxSize = 10;
        int[] orders = {23, 30, 41};
        for (int items : orders) {
            int boxes = Math.ceilDiv(items, boxSize);
            int mod = Math.ceilMod(items, boxSize);
            System.out.println(items + " items: " + boxes + " boxes, ceilMod "
                + mod + ", short by " + (-mod));
        }
    }
}

How It Works

  1. Boxes first. 23 items need 3 boxes and 41 items need 5, because ceilDiv rounds the quotient up.
  2. The gap is negative. 3 boxes hold 30, so ceilMod(23, 10) is 23 - 30 = -7; negating it gives the 7 empty places. For 41 the gap is 41 - 50 = -9.
  3. A perfect fit. 30 items fill 3 boxes exactly, so the remainder is 0 and nothing is short.

Example 2 — Compare %, floorMod, and ceilMod for Every Sign Combination

The same four pairs through three remainder operations. Watch the sign of each result. Java 18+.

Java
public class MathCeilModSigns {
    public static void main(String[] args) {
        int[][] pairs = {{7, 2}, {-7, 2}, {7, -2}, {-7, -2}};
        for (int[] p : pairs) {
            System.out.println(p[0] + ", " + p[1]
                + ": % " + (p[0] % p[1])
                + ", floorMod " + Math.floorMod(p[0], p[1])
                + ", ceilMod " + Math.ceilMod(p[0], p[1]));
        }
    }
}

How It Works

  1. The % column follows the dividend. 7 gives +1 and -7 gives -1, whatever the divisor is.
  2. The floorMod column follows the divisor. With divisor 2 the results are +1 for both 7 and -7; with divisor -2 they are -1.
  3. 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());
        }
    }
}

How It Works

  1. 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.
  2. The identity holds. -3 * 5 + (-2) is -17, so quotient and remainder always rebuild the original number.
  3. 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.

Next: clamp()

Keep a value inside a min..max range with one call (Java 21+).

clamp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful