C ++中的nextafter(x,y)函数采用两个参数,返回x之后在y方向上的下一个可表示值。
该函数在<cmath>头文件中定义。
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,则返回类型Promoted为long double。如果不是,则返回类型Promoted为double。
x:基本值。
y:近似返回值的值。
nextafter()函数返回x之后在y方向上的下一个可表示值。
#include <iostream>
#include <cmath>
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
#include <iostream>
#include <cmath>
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