Negative NaN is not a NaN?

后端 未结 6 1421
感情败类
感情败类 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 15:50

    This is based off the wikipedia article posted in the comments. Note that it's entirely untested -- it should give you an idea of something you can do though.

    bool reallyIsNan(float x)
    {
        //Assumes sizeof(float) == sizeof(int)
        int intIzedX = *(reinterpret_cast(&x));
        int clearAllNonNanBits = intIzedX & 0x7F800000;
        return clearAllNonNanBits == 0x7F800000;
    }
    

    EDIT: I really think you should consider filing a bug with the GLibc guys on that one though.

提交回复
热议问题