Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is double.NaN not equal to itself?

Tags:

c#

.net

Can someone explain this to me? In C# double.NaN is not equal to double.NaN

bool huh = double.NaN == double.NaN; // huh = false bool huh2 = double.NaN >= 0; // huh2 = false bool huh3 = double.NaN <= 0; // huh3 = false 

What constant can I compare to a double.NaN and get true?

like image 800
Carlo Avatar asked Jul 17 '09 20:07

Carlo


People also ask

Why does NaN not equal itself?

Yeah, a Not-A-Number is Not equal to itself. But unlike the case with undefined and null where comparing an undefined value to null is true but a hard check(===) of the same will give you a false value, NaN's behavior is because of IEEE spec that all systems need to adhere to.

What is the value of Double NaN?

NaN values are tested for equality by using the == operator, the result is false. So, no matter what value of type double is compared with double. NaN, the result is always false.

What is Double NaN C#?

The Double. IsNaN() method in C# is used to return a value that indicates whether the specified value is not a number (NaN).


1 Answers

If you are curious, this is what Double.IsNaN looks like:

public static bool IsNaN(double d) {     return (d != d); } 

Funky, huh?

like image 156
Erich Mirabal Avatar answered Sep 25 '22 23:09

Erich Mirabal