nextafter(x,y)函数详解

首页 / C++入门教程 / nextafter(x,y)函数详解

nextafter()函数表示特定方向上的下一个可表示值。

假设两个数字是"from"和"to" 。因此,nextafter()函数将在" to"方向上找到" from"的下一个值。

nextafter - 语法

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);

Note: 如果任何参数为long double,则返回类型为long double。如果不是,则返回类型为double。

nextafter - 参数

(from,to):这些是浮点值。

nextafter - 返回值

  • 如果" from"等于" to",则返回" from"的值。
  • 如果没有错误发生,则返回下一个可表示的'from'值。

nextafter - 例子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'。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-nextafter-function.html

来源:LearnFk无涯教程网

nextafter - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

机器学习40讲 -〔王天一〕

Flutter核心技术与实战 -〔陈航〕

后端技术面试 38 讲 -〔李智慧〕

Service Mesh实战 -〔马若飞〕

Django快速开发实战 -〔吕召刚〕

物联网开发实战 -〔郭朝斌〕

性能优化高手课 -〔尉刚强〕

攻克视频技术 -〔李江〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

好记忆不如烂笔头。留下您的足迹吧 :)