C nextafter
Move to the next representable floating-point value toward positive infinity.
A simple example
nextafter
- Start with 1.0
- Do this Take one double-precision step upward
- Result 1.0000000000000002
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.17g\n", (double) (nextafter(1.0, 2.0)));
}
1.0000000000000002
Syntax
nextafter(1.0, INFINITY) > 1.0
Run these examples in C
Save one example at a time as lesson.c. Use C17 and a Unix-style shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lessonTest 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.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%s\n", (nextafter(1.0, INFINITY) > 1.0) ? "yes" : "no");
return 0;
}
yes
The next representable value above one passes the strict greater-than check.
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.