std::isnan
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
bool isnan( float num ); bool isnan( double num ); |
(C++11 起) (C++23 前) |
|
constexpr bool isnan( /*floating-point-type*/ num ); |
(C++23 起) | |
SIMD 重载 (C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr typename /*deduced-simd-t*/<V>::mask_type |
(S) | (C++26 起) |
定义于头文件 <cmath> |
||
template< class Integer > bool isnan( Integer num ); |
(A) | (C++11 起) (C++23 起 constexpr) |
1) 确定给定的浮点数 num 是否为非数字 (NaN) 值。 库为所有 cv-无限定浮点类型提供重载作为参数 num 的类型。(C++23 起)
S) SIMD 重载对 v_num 执行元素级的
std::isnan 。
|
(C++26 起) |
A) 为所有整数类型提供了附加重载,这些整数类型被视为 double。
目录 |
[编辑] 参数
num | - | 浮点数或整数值 |
v_num | - | std::basic_simd 特化的数据并行对象,其元素类型为浮点类型 |
[编辑] 返回值
1) 如果 num 是 NaN,则为 true,否则为 false。
S) 一个数据并行掩码对象,其中如果 v_num[i] 是 NaN,则第 i 个元素等于 true,否则为 false,对于范围
[
0,
v_num.size())
中的所有 i。[编辑] 注解
存在许多不同的 NaN 值,它们具有不同的符号位和有效载荷,请参见 std::nan 和 std::numeric_limits::quiet_NaN。
NaN 值永远不等于自身或其他 NaN 值。根据 IEEE-754 标准,复制 NaN 不需要保留其位表示(符号和有效载荷),尽管大多数实现都这样做。
另一种测试浮点值是否为 NaN 的方法是将其与自身比较:bool is_nan(double x) { return x != x; }。
GCC 和 Clang 支持 -ffinite-math
选项(也由 -ffast-math
隐含),这允许相应的编译器假设不存在特殊的 IEEE-754 浮点值,例如 NaN、无穷大或负零。换句话说,在此选项下,std::isnan
始终被假定为返回 false。
不需要完全按照 (A) 提供附加重载。它们只需要足以确保对于它们的整数类型参数 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
[编辑] 参见
(C++11)(C++11)(C++11) |
非数字 (NaN) (函数) |
(C++11) |
分类给定的浮点值 (函数) |
(C++11) |
检查给定的数字是否具有有限值 (函数) |
(C++11) |
检查给定的数字是否为无穷大 (函数) |
(C++11) |
检查给定的数字是否为正规数 (函数) |
(C++11) |
检查两个浮点值是否无序 (函数) |
C 文档 for isnan
|