Negative NaN is not a NaN?

后端 未结 6 1420
感情败类
感情败类 2020-12-09 15:43

While writing some test cases, and some of the tests check for the result of a NaN.

I tried using std::isnan but the assert failes:

Asse         


        
6条回答
  •  庸人自扰
    2020-12-09 16:09

    This looks like a bug in your library's implementation of isnan() to me. It works fine here on gcc 4.2.1 on Snow Leopard. However, what about trying this?

    std::isnan(std::abs(yourNanVariable));
    

    Obviously, I can't test it, since std::isnan(-NaN) is true on my system.

    EDIT: With -ffast-math, irrespective of the -O switch, gcc 4.2.1 on Snow Leopard thinks that NAN == NAN is true, as is NAN == -NAN. This could potentially break code catastrophically. I'd advise leaving off -ffast-math or at least testing for identical results in builds using and not using it...

提交回复
热议问题