C++ Math nextafter()
C++ Math nextafter()
nextafter()函数表示特定方向上的下一个可表示值。
假设两个数字是'从"和"到" 。因此,nextafter()函数将在" to"方向上找到" from"的下一个值。
语法
float nextafter( float from, float to);
double nextafter( double from, double to);
long double nextafter( long double from, long double to);
promoted nextafter( arithmetic from, arithmetic to);
注意: 如果任何参数为long double,则返回类型为long double。如果不是,则返回类型为double。
参数
(从,到): 这些是浮点值。
返回值
如果" from"等于" to",则返回" from"的值。
如果没有错误发生,则返回下一个可表示的'from'值。
示例1
让我们看一个简单的示例,当'from'和'to'的值相等时。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float from=6.7;
float to=6.7;
cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
cout<<nextafter(from,to);
return 0;
}
输出:
Values of from and to are:6.7, 6.7
6.7
在上面的示例中," from"和" to"的值相等。因此,该函数返回'from'的值。
示例2
让我们看一个简单的示例,其中" from"和" to"是同一类型
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double from=0.0;
double to=6.0;
cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
cout<<nextafter(from,to);
return 0;
}
输出:
Values of from and to are:0, 6
4.94066e-324
在上面的示例中," from"和" to"是同一类型,但不相等。 nextafter()函数返回值,即4.94066e
-324