Kotlin java.lang.Math.ceilMod
Ceiling modulus gives the signed residual after rounding the quotient upward.
A simple example
java.lang.Math.ceilMod
- Start with 7 and 3
- Do this Use the ceiling quotient 3
- Result −2
fun main() {
println(java.lang.Math.ceilMod(7, 3))
}
-2
Syntax
java.lang.Math.ceilMod(orders, capacity)
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarMeasure 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.
fun main() {
val orders = 17
val capacity = 4
println(java.lang.Math.ceilMod(orders, capacity))
}
-3
Seventeen items need room for twenty. The residual is 17 minus 20, or minus three.
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.
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.
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.