Library contents

Java Math.ceilMod

Ceiling modulus gives the signed residual after rounding the quotient upward.

A simple example

Math.ceilMod

  1. Start with 7 and 3
  2. Do this Use the ceiling quotient 3
  3. Result −2
The remainder is 7 minus 3 times 3, which gives −2.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.ceilMod(7, 3));
    }
}
Output
-2

Syntax

Java syntax
Math.ceilMod(orders, capacity)
JDK 21 method signatures (3)
public static int ceilMod(int x, int y)
public static int ceilMod(long x, int y)
public static long ceilMod(long x, long y)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Measure the gap to the next full crate

A dispatch team packs an order into crates of a fixed capacity. Work out how the order fits the packing rule when the quantity does not divide evenly.

From the programint orders = 17
Result-3
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        int orders = 17;
        int capacity = 4;
        System.out.println(Math.ceilMod(orders, capacity));
    }
}
Output
-3

Seventeen items need room for twenty. The residual is 17 minus 20, or minus three.

Advertisements

Locate a complete carousel turn

A carousel holds six numbered trays. After twelve forward steps the indexing routine checks the remainder at a complete turn.

Given12 steps · 6 trays
Worked result0

An exact number of turns gives zero with either rounding convention.

Measure the gap in the last tray

A bakery puts 14 rolls in trays holding five. Its report expresses the last tray’s gap as the signed difference from the next full capacity.

Given14 rolls · tray size 5
Worked result−1

The ceiling quotient is three trays. Fourteen minus their capacity of fifteen is minus one.

What to watch for

With a positive divisor the residual is zero or negative. Negate it to display unused capacity.

Documentation for Math.ceilMod