gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:18:35 26 4
gpt4 key购买 nike

我正在使用 -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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com