C++官网参考链接:https://cplusplus.com/reference/cmath/isnan/
宏/函数
<cmath> <ctgmath>
isnan
C99
宏
isnan(x)
C++11
函数
bool isnan(float x);
bool isnan(double x);
bool isnan(long double x);
不是一个数
返回x是否为NaN(不是一个数)值。
NaN值用于为浮点元素标识未定义或不可表示的值,例如负数的平方根或0/0的结果。
C99
在C语言中,这被实现为一个返回int值的宏。x的类型应为float、double或long double。
C++11
在C++中,它是通过每个浮点类型(floating-point type)的函数重载来实现的,每个浮点类型都返回bool值。
参数
x
一个浮点值。
返回值
如果x是NaN值,则是非0值(true);否则为0(false)。
用例
/* isnan example */
#include <stdio.h> /* printf */
#include <math.h> /* isnan, sqrt */
int main()
{
printf ("isnan(0.0) : %d\n",isnan(0.0));
printf ("isnan(1.0/0.0) : %d\n",isnan(1.0/0.0));
printf ("isnan(-1.0/0.0) : %d\n",isnan(-1.0/0.0));
printf ("isnan(sqrt(-1.0)): %d\n",isnan(sqrt(-1.0)));
return 0;
}
输出:
另请参考
isfinite Is finite value (macro) (是有限值(宏))
isinf Is infinity (macro/function) (是无穷值(宏/函数))
isnormal Is normal (macro/function) (是正常的(宏/函数))
fpclassify Classify floating-point value (macro/function) (分类浮点值(宏/函数))