C ++中的nexttoward(x,y)函數采用兩個參數,返回x之后y方向上的下一個可表示值。
該函數在<cmath>頭文件中定義。
它與nextafter()相同,除了nexttoward()的第二個參數始終為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()函數獲得兩個參數,并返回類型的值double,float或long double類型。
x:基本值。
y:近似返回值的值。
nexttoward()函數在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