Mingw32 std :: isnan和-ffast-math [英] Mingw32 std::isnan with -ffast-math

查看:220
本文介绍了Mingw32 std :: isnan和-ffast-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



编译时判断浮点数是否为NaN?注意:在linux上,std :: isnan甚至可以使用-ffast-math。

解决方案

- ffast-math 指示GCC不处理 NaN s,可以预计 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< 1> 0xff000000u;
}
#endif


I am compiling the following code with the -ffast-math option:

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

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

I am getting 0 as output. How can my code tell whether a floating point number is NaN when it is compiled with -ffast-math?

Note: On linux, std::isnan works even with -ffast-math.

解决方案

Since -ffast-math instructs GCC not to handle NaNs, it is expected that isnan() has an undefined behaviour. Returning 0 is therefore valid.

You can use the following fast replacement for 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

这篇关于Mingw32 std :: isnan和-ffast-math的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆