Java Math.nextUp
Move to the next representable floating-point value toward positive infinity.
A simple example
Math.nextUp
- Start with 1.0
- Do this Take one double-precision step upward
- Result 1.0000000000000002
class Main {
public static void main(String[] args) throws Exception {
System.out.println(Math.nextUp(1.0));
}
}
1.0000000000000002
Syntax
Math.nextUp(limit)
JDK 21 method signatures (2)
public static double nextUp(double d)
public static float nextUp(float f)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaTest just above 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.
class Main {
public static void main(String[] args) throws Exception {
double limit = 1.0;
System.out.println(Math.nextUp(limit));
}
}
1.0000000000000002
The printed value is the closest double greater than one. It is useful for testing a strict upper boundary.
Test the scale just above 2
A scale switches display ranges at 2. The tester needs the nearest representable double above this threshold to exercise the boundary.
There is no intervening double between the threshold and this result.
Test the exposure meter just above 8
A exposure meter switches display ranges at 8. The tester needs the nearest representable double above this threshold to exercise the boundary.
There is no intervening double between the threshold and this result.
What to watch for
The step is a representation limit, not a fixed engineering tolerance.