C/C++/C# nan如何判斷

扣扣技術交流羣:460189483

注意:nan是無序的(unordered),無法對其進行邏輯運算。它不大於、小於或等於任何數(包括它自己),將<,>,<=,和>=作用於nan產生一個exception。得到nan時就查看是否有非法操作,如果表達式中含有nan,那麼表達式的結果爲nan。

 NAN的判斷

在math.h頭文件中有判斷是否爲nan的函數,isnan(x) 如果是nan返回1,如果不是返回0

#include <math.h>

typedef union{
    float x;
    u8 buf[4];
}nan_t;

int main()
{
    nan_t tmp;
    tmp.buf[0] = 255;
    tmp.buf[1] = 255;
    tmp.buf[2] = 255;
    tmp.buf[3] = 255;

    if(isnan(tmp.x))
    {
        printf("this is nan");
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章