Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equal to operator == can be used to do checking bit ?

What is the purpose of the function ?

 bool whatIsIt(double n)
 {
    return n == n;
 }

It can be used to check every bit in n ?

I doubt that .

Any comments are appreciated.

like image 908
user1000107 Avatar asked Dec 04 '22 06:12

user1000107


2 Answers

It could be used to check if n is NaN (not a number), since NaN does not compare equal to itself.

Probably a finnicky and not entirely reliable way to do it. (see Billy's various comments) C99 and C++11 have the isnan() function.

like image 180
Benjamin Lindley Avatar answered Dec 05 '22 21:12

Benjamin Lindley


This is specified in the C Standard in Annex F: 60559 Floating Point Arithmetic, specifically F.8.3 Relational operators:

... The statement x != x is true if x is a NaN

... The statement x == x is false if x is a NaN

If __STDC_IEC_559__ is #defined, then this function will return false if n is NaN.

like image 45
MSN Avatar answered Dec 05 '22 20:12

MSN