📜  C++ nexttoward()

📅  最后修改于: 2020-09-25 08:00:29             🧑  作者: Mango

C++中的nexttoward() 函数采用两个参数,并在y方向上返回x之后的下一个可表示值。

该函数在头文件中定义。

它与nextafter()相同,除了nexttoward()的第二个参数始终为long double类型。

nexttoward()原型[从C++ 11标准开始]

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

nexttoward() 函数接受两个参数,并返回doublefloatlong double类型的值。

nexttoward()参数

nexttoward()返回值

nexttoward() 函数在y方向上返回x之后的下一个可表示值。

示例1:nexttoward() 函数在C++中如何工作?

#include 
#include 

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;
}

运行该程序时,输出为:

nexttoward(x, y) = -4.94066e-324

示例2:整数类型的nexttoward() 函数

#include 
#include 
#include 

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;
}

运行该程序时,输出为:

nexttoward(x, y) = 2.14748e+09