C++ cmath nexttoward() Function
Example
Get the double value just before 1.0:
double x = nexttoward(1.0, 0.0L);
if (x < 1.0) {
cout << "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 <cmath> header file.
Syntax
nexttoward(double x, long double y)
The function also works with a float or long double value for x. Then it returns a value of the same type.
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 Math Tutorial.