📜  C++ nextafter()

📅  最后修改于: 2020-09-25 07:59:42             🧑  作者: Mango

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

该函数在头文件中定义。

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

double nextafter(double x, double y);
float nextafter(float x, float y);
long double nextafter(long double x, long double y);
Promoted nextafter(Type1 x, Type2 y); // Additional overloads

从C++ 11开始,如果传递给nextafter()的任何参数为long double ,则返回类型Promotedlong double 。如果不是,则返回类型Promoteddouble

nextafter()参数

nextafter()返回值

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

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

#include 
#include 

using namespace std;

int main()
{
    double x = 0.0, y = 1.0;

    double resultInDouble = nextafter(x,y);
    cout << "nextafter(x, y) = " << resultInDouble << endl;

    return 0;
}

运行该程序时,输出为:

nextafter(x, y) = 4.94066e-324

示例2:nextafter() 函数用于不同类型的参数

#include 
#include 

using namespace std;

int main()
{
    float y = 1.0;
    double x = INFINITY;

    double result = nextafter(x,y);
    cout << "nextafter(x, y) = " << result << endl;

    return 0;
}

运行该程序时,输出为:

nextafter(x, y) = 1.79769e+308