Why is double.NaN not equal to itself?
Asked Answered
G

11

88

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?

Greataunt answered 17/7, 2009 at 20:12 Comment(6)
Just to explain your huhs : NaN is equal to nothing, not even itself. This is by definition. en.wikipedia.org/wiki/NaNAquavit
What I think is unfortunate is that the context is lost. If we had two doubles, and both were assigned a value of NaN in order to represent the actual value 1/0. They should be equal, but since the context is lost, they're treated as not equalTwobit
You are right, an extra line of code should have to be implemented for that particular case.Greataunt
mathematically correct. Why would one think one nan would be equal to another? sqrt( -1 ) != 1/0Mill
It is just like NULL in SQLYeager
@MichaelMeadows 1/0 is Inf, not NaN.Tuinenga
C
142

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

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

Funky, huh?

Caucasia answered 17/7, 2009 at 20:15 Comment(12)
That is weird allright. But then again, so is the declaration of NaN: public const double NaN = (double) 1.0 / (double) 0.0;Kr
@Fedrik: as funky as this looks, you really should play with imitating that definition. You will get different results if you use double.NaN and double myNaN = (double)1.0 / (double)0.0; as the test (especifically, the former will test as false for IsInfinity and the latter as true!)Caucasia
@Fredik, @Erich: Division by Zero will give inf (or, +inf, -inf based on the operands), 0 / 0 (among others) results in a NaN. There's a nice table with special operations/results at steve.hollasch.net/cgindex/coding/ieeefloat.htmlUnrequited
@Erick Mirabal: BTW, where did you find out what IsNaN looks like? There's some times I need to see the implementation of certain .net methods.Greataunt
@Greataunt You can use .NET Reflector or use the Microsoft source server when debugging.Zenger
@Torsten: yes, I know division by zero gives you Infinity. But what I find funky is that double.NaN is defined as a number divided by zero, yet it fails the IsInfinity test! @Carlo, as Adrian notes, .NET Reflector is what I used. I use it all the time to check implementation ideas and details.Caucasia
To add to the confusion, object.Equals(double.NaN, double.NaN) returns trueHearthside
@FredrikMörk "so is the declaration of NaN:" -- why lie? This answer gave the actual definition of IsNaN, whereas you just made something up.Tuinenga
"double.NaN is defined as a number divided by zero" -- no it isn't. 0/0 has no mathematical meaning and is thus "not a number"; there are many other ways to get "non a number".Tuinenga
@JimBalter You are correct that my comment was wrong: the reference source is actually saying public const double NaN = (double)0.0 / (double)0.0; link to the reference source. Thanks for pointing that out. On another note: being wrong and lying are two completely different thingsKr
@FredrikMörk As I said, you just made it up; you never saw any such declaration. Therefore it was a lie, not merely wrong.Tuinenga
Not sure if this was correct when you posted it, but IsNaN is defined differently today.Jespersen
Z
47

Use Double.IsNaN.

Zenger answered 17/7, 2009 at 20:14 Comment(0)
L
17
bool isNaN = Double.IsNaN(yourNumber)
Lutist answered 17/7, 2009 at 20:14 Comment(0)
C
13

The behavior is on purpose. The reason being NaN represents something that is not a number and so that is sort of a catch-all for many things.

The proper way to compare something to being NaN is to use the IsNaN function.

Cosma answered 17/7, 2009 at 20:15 Comment(0)
S
8

Use Double.IsNan() to test for equality here. The reason is that NaN is not a number.

Santana answered 17/7, 2009 at 20:15 Comment(0)
A
7

There's a specialized function for this:

double.IsNan(huh);
Albi answered 17/7, 2009 at 20:15 Comment(0)
B
5

Use the method "Double.IsNaN( value )" to check for this condition.

Boltonia answered 17/7, 2009 at 20:15 Comment(0)
U
3

Actually, you already found the way to check if a IEEE-754 floating point number is NaN: it is the only floating point value (or range of values, because there are several NaNs) that evaluates to False if compared to itself, i.e. :

bool isNaN(double v) {
    return v != v;
}

Under the hood, the Double.IsNaN method might actually do the same thing. You should still use it, because the behavior is quite surprising to anybody who does not know about the FP standard.

Unrequited answered 17/7, 2009 at 20:23 Comment(0)
T
3

The only thing that we know about NaN is that it's "Not a Number." That doesn't mean that it has a value that is associable with its state. For example:

∞ + (-∞) = NaN

0/0 = NaN

(∞ + (-∞)) <> (0/0)

Here's some C# to demonstrate

var infinity = 100d / 0;
var negInfinity = -100d / 0;

var notANumber = infinity + negInfinity;
Console.WriteLine("Negative Infinity plus Infinity is NaN: {0}", double.IsNaN(notANumber));

var notANumber2 = 0d / 0d;
Console.WriteLine("Zero divided by Zero is NaN: {0}", double.IsNaN(notANumber2));

Console.WriteLine("These two are not equal: {0}", notANumber == notANumber2);
Twobit answered 17/7, 2009 at 20:43 Comment(0)
C
2

The reason of Double.NaN != Double.NaN is simple:

Do you expect 0/0 to be the same as Math.Sqrt(-3)? And same as Math.Sqrt(-7)?

There is a bug in C# in my opinion where Equals() is not overridden for NaN.

Assert.IsTrue(Double.NaN != Double.NaN);
Assert.IsTrue(Double.NaN.Equals(Double.NaN));

At the same time

Assert.IsTrue(Double.PositiveInfinity == Double.NegativeInfinity);
Assert.IsTrue(Double.PositiveInfinity.Equals(Double.PositiveInfinity));
// same for Double.NegativeInfinity and Single

Use static functions for Double and Single, e.g.

Double.IsNaN(value) && Double.IsInfinity(value);

Or more specific:

Double.IsPositiveInfinity(value);
Double.IsNegativeInfinity(value);
Cockboat answered 11/3, 2014 at 4:21 Comment(0)
V
2

The Equality operator considers two NaN values to be unequal to one another. In general, Double operators cannot be used to compare Double.NaN with other Double values, although comparison methods (such as Equals and CompareTo) can. see below examples

Referenced from msdn

class Program
{
    static void Main(string[] args)
    {
        Double i = Double.NaN;
        while (!i.Equals(i)) //this would be result in false
        //while(i != i) // this would result in true.
        {
            Console.WriteLine("Hello");
        }
    }
}

here is .net fiddle for the same.

Vaginismus answered 18/11, 2015 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.