Groovy Math.nextAfter
Move one representable floating-point value in the direction of a target.
A simple example
Math.nextAfter
- Start with 1.0
- Do this Take one double-precision step upward
- Result 1.0000000000000002
println(Math.nextAfter(1.0d, 2.0d))
1.0000000000000002
Syntax
Math.nextAfter(reading, target)
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovyStep toward a calibration target
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.
double reading = 1.0
double target = 2.0
println(Math.nextAfter(reading, target))
1.0000000000000002
Starting at one and aiming for two advances only to the next double above one.
Probe a lamp setting from below
The lamp controller starts at two units. A test requests one floating-point step in the direction of three to check the upper neighbour.
Using 2 · 3 gives 2.0000000000000004 for this measurement.
Probe a limit from above
A meter reads eight units before calibration. The software takes one representable step toward zero to exercise the lower side of the limit.
Using 8 · 0 gives 7.999999999999999 for this measurement.
What to watch for
The target selects the direction. This method never jumps the full distance unless the values are adjacent or equal.