Mingw32 std :: isnan with -ffast-math

And*_*uel 2 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;
}
Run Code Online (Sandbox Code Playgroud)

我输出0.在编译时,我的代码如何判断浮点数是否为NaN -ffast-math

注意:在linux上,std :: isnan甚至可以使用-ffast-math.

sam*_*var 10

由于-ffast-math指示GCC不处理NaNs,因此预期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
Run Code Online (Sandbox Code Playgroud)