Checking if a double (or float) is NaN in C++

后端 未结 21 2154
北恋
北恋 2020-11-22 05:10

Is there an isnan() function?

PS.: I\'m in MinGW (if that makes a difference).

I had this solved by using isnan() from , which doe

21条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:31

    A possible solution that would not depend on the specific IEEE representation for NaN used would be the following:

    template
    bool isnan( T f ) {
        T _nan =  (T)0.0/(T)0.0;
        return 0 == memcmp( (void*)&f, (void*)&_nan, sizeof(T) );
    }
    

提交回复
热议问题