Library contents

Scala math.floor

Scala math.floor returns the greatest integer-valued Double less than or equal to its argument.

A simple example

math.floor

  1. Start with 2.8
  2. Do this Round downward to a whole number
  3. Result 2
Two is the greatest integer at or below 2.8.
Lesson.scala
object Lesson extends App {
    println(math.floor(2.8))
}
Output
2.0

Syntax

Scala syntax
math.floor(value)
Run this example in Scala

Save the program as Lesson.scala. Use Scala 2.13.

scalac Lesson.scala
scala Lesson

Assign negative readings to intervals

A measurement below zero falls between two numbered intervals. Work through how negative values are assigned so the interval on the lower side of zero is labeled consistently with the positive side.

First output line-1.0 0
Lesson.scala
object Lesson {
  def main(args: Array[String]): Unit = {
    for (reading <- List(-0.2, 0.0, 1.0)) {
      println(math.floor(reading).toString + " " + reading.toInt)
    }
  }
}
Output
-1.0 0
0.0 0
1.0 1

A negative fraction falls in the interval beginning at −1. Converting it directly to Int truncates toward zero, producing a different result for negative nonintegers.

Scala documentation for math.floor

Advertisements

Cut complete straps from a roll

The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.

Given8.5 m of ribbon · 1.2 m per strap
Result7 straps
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val metres = 8.5
        val perStrap = 1.2
        val straps = math.floor(metres / perStrap).toInt
        println((straps).toString() + " straps")
    }
}
Output
7 straps

Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.

Make complete paint sets

A studio has 29 paint tubes and puts six in each set. It lists only sets that can be handed out complete.

Given29 tubes · 6 per set
Worked result4 sets

Four sets use 24 tubes. Five tubes stay on the shelf.