g++ - Mingw32 std::isnan 与 -ffast-math

标签 g++ mingw32 fast-math

我正在使用 -ffast-math 选项编译以下代码:

#include <limits>
#include <cmath>
#include <iostream>

int main() {
    std::cout << std::isnan(std::numeric_limits<double>::quiet_NaN() ) << std::endl;
}

我得到 0 作为输出。当我的代码使用 -ffast-math 编译时,如何判断 float 是否为 NaN?

注意:在 Linux 上,std::isnan 甚至可以与 -ffast-math 一起使用。

最佳答案

由于 -ffast-math 指示 GCC 不要处理 NaN,因此预计 isnan() 具有未定义的行为。因此,返回 0 是有效的。

您可以使用以下快速替换 isnan():

#if defined __FAST_MATH__
#   undef isnan
#endif
#if !defined isnan
#   define isnan isnan
#   include <stdint.h>
static inline int isnan(float f)
{
    union { float f; uint32_t x; } u = { f };
    return (u.x << 1) > 0xff000000u;
}
#endif

关于g++ - Mingw32 std::isnan 与 -ffast-math,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7263404/

相关文章:

C++ 隐式返回返回类型变量

c++ - sizeof struct gcc 编译成功,但 g++ 编译失败

c++ - C 库链接到 C++

c++ - 如何在面向对象的 C++ 中创建新的数据结构?

c - 与-O3相比,gcc -Ofast的汇编代码中计算不精确的来源在哪里?

c++ - std::isinf不适用于-ffast-math。如何检查无穷大

c++ - 如何构建简单的 OpenCV 程序

c++ - 在 Windows 7 上安装 QCA-OSSL(Qt 加密体系结构的一部分)插件时出现问题

c++ - Qt windeployqt 导致部署不可用

c++ - 为什么在使用快速数学时 GCC 或 Clang 不优化 1 条指令的倒数