Library contents

Scala java.lang.Math.floorDivExact

Divide two signed integers and round the quotient down while checking for overflow.

A simple example

java.lang.Math.floorDivExact

  1. Start with −7 divided by 3
  2. Do this Round the quotient down
  3. Result −3
The quotient lies between −3 and −2. Its floor is the lower integer, −3.
Lesson.scala
object Lesson extends App {
    println(java.lang.Math.floorDivExact(-7, 3))
}
Output
-3

Syntax

Scala syntax
java.lang.Math.floorDivExact(shift, period)
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

Find a time slot before a schedule starts

A shift planner numbers intervals relative to opening time. Assign a time before opening to the correct earlier slot rather than losing the sign at the boundary.

From the programval shift = -7
Result-3
Lesson.scala

object Lesson {
    def main(args: Array[String]): Unit = {
        val shift = -7
        val period = 3
        println(java.lang.Math.floorDivExact(shift, period))
    }
}
Output
-3

An event seven minutes before the start falls in slot minus three when slots are three minutes wide.

Advertisements

Locate an event before opening

A desk groups arrivals into ten-minute blocks, with block zero beginning at opening time. A volunteer arrived one minute before opening and needs the correct block label.

GivenMinute −1 · block width 10
Worked result−1

Floor division assigns the minute immediately before zero to block minus one.

Group an afternoon reading

A meter logs one entry per five-minute block starting at noon. A reading comes in seventeen minutes after noon.

GivenMinute 17 · block width 5
Worked result3

Block three covers minutes fifteen through nineteen.

What to watch for

A zero divisor and the minimum signed value divided by minus one raise ArithmeticException.

Documentation for java.lang.Math.floorDivExact