isnan()函数详解

首页 / C++入门教程 / isnan()函数详解

该函数检查数字是否为非数字。如果数字为NaN,则返回1,否则返回0。

Note: 对于浮点元素(例如负数的平方根或0/0的结果),NaN是无法表示的值。

isnan - 语法

假设数字是“ x”。语法为:

bool isnan(float x);
bool isnan(double x);
bool isnan(long double x);
bool isnan(integral x);

isnan - 参数

x :这是一个浮点值。

isnan - 返回值

如果x为NAN,则返回1,否则返回0。

isnan - 例子1

让我们看一下x的值为0.0/0.0时的简单示例。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float x=0.0/0.0;  
    cout<<"value of x is : "<<x<<'\n';  
    cout<<"isnan(x) : "<<isnan(x);  
    return 0;  
}  

输出:

value of x is : -nan
isnan(x) : 1   

在此示例中,isnan(x)确定x的值为nan。因此,它返回1。

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

来源:LearnFk无涯教程网

isnan - 例子2

让我们看一下x值为4.3时的简单示例。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float x=4.3;  
    cout<<"value of x is : "<<x<<'\n';  
    cout<<"isnan(x) : "<<isnan(x);  
    return 0;  
}  

输出:

value of x is : 4.3
isnan(x) : 0   

在此示例中,isnan(x)函数确定x的值不是'nan'。因此,它返回0值。

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

技术教程推荐

全栈工程师修炼指南 -〔熊燚(四火)〕

跟月影学可视化 -〔月影〕

动态规划面试宝典 -〔卢誉声〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

说透数字化转型 -〔付晓岩〕

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

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

AI大模型企业应用实战 -〔蔡超〕

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