C ++中的nexttoward(x,y)函數(shù)采用兩個參數(shù),返回x之后y方向上的下一個可表示值。
該函數(shù)在<cmath>頭文件中定義。
它與nextafter()相同,除了nexttoward()的第二個參數(shù)始終為type long double。
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()函數(shù)獲得兩個參數(shù),并返回類型的值double,float或long double類型。
x:基本值。
y:近似返回值的值。
nexttoward()函數(shù)在y方向上返回x之后的下一個可表示值。
#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;
}運行該程序時,輸出為:
nexttoward(x, y) = -4.94066e-324
#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;
}運行該程序時,輸出為:
nexttoward(x, y) = 2.14748e+09