English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++nexttoward() Function Usage and Example
The nexttoward(x, y) function in takes two parameters and returns the next representable value after x in the y direction.<cmath>The function in
It is defined in the header file.nextafter()The same, except that the second parameter of nexttoward() is always of type long double.
double nexttoward(double x, long double y); float nexttoward(float x, long float y); long double nexttoward(long double x, long double y); double nexttoward(T x, long double y); // For integral type
The nexttoward() function takes two parameters and returns a value of type double, float, or long double.
x:Basic value.
y:Approximate the value of the return value.
The nexttoward() function returns the next representable value after x in the y direction.
#include <iostream> #include <cmath> using namespace std; int main() { long double y = -1.0; double x = 0.0; double result = nexttoward(x, y); cout << "nexttoward(x, y) = " << result << endl; return 0; }
When running the program, the output is:
nexttoward(x, y) = -4.94066e-324
#include <iostream> #include <cmath> #include <climits> using namespace std; int main() { long double y = INFINITY; int x = INT_MAX; double result = nexttoward(x,y); cout << "nexttoward(x, y) = " << result << endl; return 0; }
When running the program, the output is:
nexttoward(x, y) = 2.14748e+09