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