C ++中的nextafter(x,y)函數(shù)采用兩個參數(shù),返回x之后在y方向上的下一個可表示值。
該函數(shù)在<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()的參數(shù)為long double,則返回類型Promoted為long double。如果不是,則返回類型Promoted為double。
x:基本值。
y:近似返回值的值。
nextafter()函數(shù)返回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