In C, nexttoward() returns the next representable value of x in the direction of y. It is like nextafter(), but the direction argument is a long double.
You pass a double start value and a long double target direction. The result is a double. Include <math.h>. On GCC, link with -lm.
Use it when your direction comes from a wider type, or when you want the same stepping idea as nextafter with a long double goal.
Syntax
Pass the start value, then the long double direction. Start with this call.
C
nexttoward(1.0, 0.0L);
Call
What it does
nexttoward(1.0, 0.0L);
Returns the next double from 1.0 toward 0.
Include <math.h>. On GCC, link with -lm. The second argument is long double, not double.
Library declarations
C
double nexttoward(double x, long double y);
float nexttowardf(float x, long double y);
long double nexttowardl(long double x, long double y);
Parameters
Parameter
Description
x
Required. The double (or matching float type) to step from.
y
Required. A long double that sets the direction to move toward.
Return Value
Return Value
Description
Next value
The next representable value of x in the direction of y.
x unchanged
If x already equals y (as floating values), x is returned.
C Examples
Example 1 — Basic Direction Step
Move from 1.0 toward a smaller long double goal.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 1.0;
double y = nexttoward(x, 0.0L);
printf("x = %.17g\n", x);
printf("next = %.17g\n", y);
return 0;
}
Output
x = 1
next = 0.99999999999999989
How It Works
Include math.h. That declares nexttoward.
Start at 1.0. A simple double.
Aim toward 0.0L. The L suffix makes a long double.
One step. The result is the next double below 1.
Example 2 — Step Toward a Larger Goal
When the direction is larger than x, the result moves up.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 1.0;
double y = nexttoward(x, 10.0L);
printf("%.17g\n", y);
printf("greater? %s\n", y > x ? "yes" : "no");
return 0;
}
Output
1.0000000000000002
greater? yes
How It Works
Same start. Still begin at 1.0.
Larger y.10.0L points upward.
Result grows. You get the next double above 1.
Tiny bump. One ulp is a very small change.
Example 3 — Side by Side with nextafter
Show that both APIs can take the same conceptual step when the goal matches.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double x = -4.0;
double a = nexttoward(x, 0.0L);
double b = nextafter(x, 0.0);
printf("nexttoward = %.17g\n", a);
printf("nextafter = %.17g\n", b);
return 0;
}
Compare results. Printed values match on typical hosts.
Type detail. Only the direction argument’s type differs.
Frequently Asked Questions
nexttoward(x, y) returns the next representable value of x in the direction of y. y is a long double.
x is the start value. y is the long double direction to move toward. Both are required.
A value with the type of x that is one step closer to y on the floating-point line.
Include <math.h>. On GCC, link with -lm.
Both step one representable value toward a goal. nexttoward always takes a long double direction; nextafter uses the same floating type for both arguments.