检查double(或float)在C ++中是否为NaN
要检查C++中的浮点数或双数是否为NaN(非数字),我们可以使用该isnan()函数。该isnan()函数存在于cmath库中。此函数在C++版本11中引入。因此,从C++11接下来,我们可以使用此函数。
示例
#include <cmath>
#include <iostream>
using namespace std;
main() {
if(isnan(sqrt(30))) { //square root of 30 is a floating point number
cout << "Square root of 30 is not a number" <<endl;
} else {
cout << "Square root of 30 is a number" <<endl;
}
if(isnan(sqrt(-30))) { //square root of -30 is an imaginary number
cout << "Square root of -30 is not a number" <<endl;
} else {
cout << "Square root of -30 is a number" <<endl;
}
}输出结果
Square root of 30 is a number Square root of -30 is not a number
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短