English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Reference Manual

C++ Library Function <cmath>

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.

nexttoward() prototype[From C ++ 11Standard begins]

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.

nexttoward() parameters

  • x:Basic value.

  • y:Approximate the value of the return value.

nexttoward() return value

The nexttoward() function returns the next representable value after x in the y direction.

Example1:The nexttoward() function in C ++How does it work?

#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

Example2:The nexttoward() function of integer type

#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

C++ Library Function <cmath>