nexttoward()
函数nexttoward()函数是cmath标头的库函数,用于在给定第二个数字的方向上获取给定第一个数字之后的下一个可表示值,它接受两个数字(x,y)并在之后返回下一个可表示值x沿y方向。
注意:nexttoward()函数与nextafter()函数类似,但它返回下一个可表示的值,其中可能包含更精确的y。
nexttoward()
函数语法:
nexttoward(x, y);
参数: x,y –是用于在y方向上查找x的下一个可表示值的数字。
返回值: float / double / long double-根据输入类型,它返回x的下一个可表示值。
示例
Input: float x = 0.0; long double y = 1.0L; Function call: nexttoward(x,y); Output: 1.4013e-45
nexttoward()
函数示例//示例 // nexttoward()功能 #include <iostream> #include <cmath> using namespace std; // main()部分 int main(){ float x; long double y; x = 0.0; y = 1.0L; cout<<"nexttoward("<<x<<","<<y<<"): "<<nexttoward(x,y); cout<<endl; x = 0.0; y = 1.1L; cout<<"nexttoward("<<x<<","<<y<<"): "<<nexttoward(x,y); cout<<endl; x = 0.0; y = -1.0L; cout<<"nexttoward("<<x<<","<<y<<"): "<<nexttoward(x,y); cout<<endl; x = 0.1; y = 0.1L; cout<<"nexttoward("<<x<<","<<y<<"): "<<nexttoward(x,y); cout<<endl; return 0; }
输出结果
nexttoward(0,1): 1.4013e-45 nexttoward(0,1.1): 1.4013e-45 nexttoward(0,-1): -1.4013e-45 nexttoward(0.1,0.1): 0.1