Library contents

Kotlin java.lang.Math.ulp

Return the spacing associated with one unit in the last place of a floating-point value.

A simple example

java.lang.Math.ulp

  1. Start with 1.0
  2. Do this Measure one unit in the last place
  3. Result 2⁻⁵²
For a double at 1.0, this is the distance to the next larger representable value.

Syntax

Kotlin syntax
java.lang.Math.ulp(reading)
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.jar

Inspect the resolution of a sensor calculation

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.

From the programval reading = 1.0
Result2.220446049250313E-16
Lesson.kt

fun main() {
    val reading = 1.0
    println(java.lang.Math.ulp(reading))
}
Output
2.220446049250313E-16

At one, the next larger double is approximately 2.22 times ten to the minus sixteen away.

Advertisements

Inspect the scale near two units

A calibration test stores a double reading of 2.0. The developer measures the gap from that value to its next larger representable reading.

Given2
Worked result4.440892098500626e-16

Using 2 gives 4.440892098500626e-16 for this measurement.

Check precision in a high range

A measurement program switches its reference level to 1024.0. The tester records how far apart neighbouring double readings are at that level.

Given1024
Worked result2.2737367544323206e-13

Using 1024 gives 2.2737367544323206e-13 for this measurement.

What to watch for

The spacing changes with magnitude. It measures representation precision rather than physical sensor accuracy.

Documentation for java.lang.Math.ulp