Scala java.lang.Math.getExponent
Extract the unbiased base-two exponent of a normal floating-point value.
A simple example
java.lang.Math.getExponent
- Start with 8.0
- Do this Read the binary exponent
- Result 3
object Lesson extends App {
println(java.lang.Math.getExponent(8.0))
}
3
Syntax
java.lang.Math.getExponent(reading)
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 LessonFind the binary scale of a sensor reading
A sensor stores readings in a binary floating-point format with limited precision. Inspect or adjust the scale of the sample value so the calibration code can account for representable steps.
object Lesson {
def main(args: Array[String]): Unit = {
val reading = 12.0
println(java.lang.Math.getExponent(reading))
}
}
3
Twelve lies between two cubed and two to the fourth, so its exponent is three.
Choose a range on a sound meter
A normalized meter reading is 20. The instrument labels its binary range with the exponent of the power of two at the bottom of the interval.
Using 20 gives 4 for this measurement.
Classify a dim light reading
A light sensor returns 0.75 in its normalized units. The graph groups positive samples by their unbiased binary exponent.
Using 0.75 gives -1 for this measurement.
What to watch for
Zero, subnormal readings, infinities, and NaN have special exponent results.