C Math nexttoward() Function
Example
Get the double value just before 1.0:
double x = nexttoward(1.0, 0.0L);
if (x < 1.0) {
printf("x is a little smaller than 1");
}
Definition and Usage
The nexttoward() function returns the closest floating point number to x in the direction of y. It works like nextafter(), but y is always a long double.
In the example, the L at the end of 0.0L makes it a long double. The result is the closest double below 1.0.
The nexttoward() function is defined in the <math.h> header file.
Syntax
double nexttoward(double x, long double y);
There are also versions for float and long double values: nexttowardf() and nexttowardl().
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to start from. |
| y | Required. A long double with the direction to go in. If y is smaller than x, the result is smaller than x. |
Technical Details
| Returns: | A double value with the closest number to x in the direction of y. |
|---|
Related Pages
Read more about math in our C Math Tutorial.