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