Scala java.lang.Math.nextDown
Move to the next representable floating-point value toward negative infinity.
A simple example
java.lang.Math.nextDown
- Start with 1.0
- Do this Take one double-precision step downward
- Result 0.9999999999999999
object Lesson extends App {
println(java.lang.Math.nextDown(1.0))
}
0.9999999999999999
Syntax
java.lang.Math.nextDown(limit)
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 LessonTest just below a measurement limit
A calibration routine needs to test the nearest representable value on one side of a limit. Move by one floating-point step so the boundary test uses a value the computer can actually store.
object Lesson {
def main(args: Array[String]): Unit = {
val limit = 1.0
println(java.lang.Math.nextDown(limit))
}
}
0.9999999999999999
The printed value is the closest double smaller than one.
Test the scale just below 2
A scale switches display ranges at 2. The tester needs the nearest representable double below this threshold to exercise the boundary.
There is no intervening double between the threshold and this result.
Test the exposure meter just below 8
A exposure meter switches display ranges at 8. The tester needs the nearest representable double below this threshold to exercise the boundary.
There is no intervening double between the threshold and this result.
What to watch for
The gap below a power of two can differ from the gap above it.