跳到主要内容

Isnan

定义于头文件 <cmath> 中。

描述

确定给定的浮点数num是否为非数字 (NaN) 值。
库为所有 cv-unqualified 浮点类型提供了重载作为参数 num 的类型  (C++23 起)

​为所有整数类型提供了附加重载,这些类型被视为double

声明

// 1)
constexpr bool isnan( /* floating-point-type */ num );
附加重载
// 2)
template< class Integer >
constexpr bool isnan( Integer num );

参数

num - 浮点或整数值

返回值

如果numNaN,则为true,否则为false

备注

有许多不同的NaN值,具有不同的符号位和有效载荷,请参见std::nanstd::numeric_limits::quiet_NaN

NaN值从不与自身或与其他NaN值比较相等。根据 IEEE-754,复制NaN不需要保留其位表示(符号和有效载荷),尽管大多数实现会这样做。

另一种测试浮点值是否为NaN的方法是将其与自身进行比较:bool is_nan(double x) { return x != x; }

不要求提供的附加重载与附加重载完全一致。它们只需足以确保对于整数类型的参数 num,
std::isnan(num)std::isnan(static_cast<double>(num))效果相同。

示例

#include <cfloat>
#include <cmath>
#include <iostream>

int main()
{
std::cout
<< std::boolalpha
<< "isnan(NaN) = "
<< std::isnan(NAN) << '\n'
<< "isnan(Inf) = "
<< std::isnan(INFINITY) << '\n'
<< "isnan(0.0) = "
<< std::isnan(0.0) << '\n'
<< "isnan(DBL_MIN/2.0) = "
<< std::isnan(DBL_MIN / 2.0) << '\n'
<< "isnan(0.0 / 0.0) = "
<< std::isnan(0.0 / 0.0) << '\n'
<< "isnan(Inf - Inf) = "
<< std::isnan(INFINITY - INFINITY) << '\n';
}

可能结果
isnan(NaN) = true
isnan(Inf) = false
isnan(0.0) = false
isnan(DBL_MIN/2.0) = false
isnan(0.0 / 0.0) = true
isnan(Inf - Inf) = true

Isnan

定义于头文件 <cmath> 中。

描述

确定给定的浮点数num是否为非数字 (NaN) 值。
库为所有 cv-unqualified 浮点类型提供了重载作为参数 num 的类型  (C++23 起)

​为所有整数类型提供了附加重载,这些类型被视为double

声明

// 1)
constexpr bool isnan( /* floating-point-type */ num );
附加重载
// 2)
template< class Integer >
constexpr bool isnan( Integer num );

参数

num - 浮点或整数值

返回值

如果numNaN,则为true,否则为false

备注

有许多不同的NaN值,具有不同的符号位和有效载荷,请参见std::nanstd::numeric_limits::quiet_NaN

NaN值从不与自身或与其他NaN值比较相等。根据 IEEE-754,复制NaN不需要保留其位表示(符号和有效载荷),尽管大多数实现会这样做。

另一种测试浮点值是否为NaN的方法是将其与自身进行比较:bool is_nan(double x) { return x != x; }

不要求提供的附加重载与附加重载完全一致。它们只需足以确保对于整数类型的参数 num,
std::isnan(num)std::isnan(static_cast<double>(num))效果相同。

示例

#include <cfloat>
#include <cmath>
#include <iostream>

int main()
{
std::cout
<< std::boolalpha
<< "isnan(NaN) = "
<< std::isnan(NAN) << '\n'
<< "isnan(Inf) = "
<< std::isnan(INFINITY) << '\n'
<< "isnan(0.0) = "
<< std::isnan(0.0) << '\n'
<< "isnan(DBL_MIN/2.0) = "
<< std::isnan(DBL_MIN / 2.0) << '\n'
<< "isnan(0.0 / 0.0) = "
<< std::isnan(0.0 / 0.0) << '\n'
<< "isnan(Inf - Inf) = "
<< std::isnan(INFINITY - INFINITY) << '\n';
}

可能结果
isnan(NaN) = true
isnan(Inf) = false
isnan(0.0) = false
isnan(DBL_MIN/2.0) = false
isnan(0.0 / 0.0) = true
isnan(Inf - Inf) = true