nan Comparison of a double with double.Nan (Not a Number) will always return false. Even (double.Nan == double.Nan) return false and indeed, from IEEE 754 specs: equality comparison between two Nan is always false. If we want to check whether a specific double is Nan, double.IsNan() shall be used. Lets look at how the IsNan method is implemented (using Reflector):

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

My question is why double.Nan exist? It only creates confusion…

Tags :

18 Responses to “Don’t Use Double.Nan”


  1. JV

    Said on April 18, 2008 :

    You already gave the answer yourself. Because they implemented IEEE 754…

  2. Gabriel Rodriguez

    Said on April 18, 2008 :

    any smart reason of why the equality comparison always returns false?

  3. Shahar Y

    Said on April 18, 2008 :

    JV,
    I am sure they didn’t implement all of the IEEE specs, so they could just ignore this one too.
    Anyway, I was just saying that this is a strange behavior. Most of the developers don’t know IEEE specs…

  4. Shahar Y

    Said on April 18, 2008 :

    Hi Gabriel Rodriguez,
    Nan could be generated by many different mathematical operations and: (Square(-1) == Log(0)) shall indeed return false. But the problem is that (Square(-1) == Square(-1)) will also return false…

  5. Mark S. Rasmussen

    Said on April 18, 2008 :

    It’s logic (literally). The same goes for NULL comparisons in any database. You cannot compare two unknowns, and that’s exactly what NaN is, it’s an unknown. All we know is that it’s definitely not a number. Other than that it could be anything.

    If I present you with two wrapped boxes and ask you to tell me if the contents are equal, what would you answer?

  6. aalmada

    Said on April 18, 2008 :

    double.NaN and double.Infinity exist for mathematical reasons. The following operations, instead of throwing exceptions, return these values:

    var x = 1.0 / 0.0; // double.Infinity
    var y = Math.Sin(0.0); // double.NaN

  7. SY

    Said on April 18, 2008 :

    Sometimes its better to throw exception if your data gets corrupted or unknown. I do think it is much better then using NANs.

  8. Casper Bang

    Said on April 18, 2008 :

    On a related note, I find it a much bigger problem in practice, that Double.MIN_VALUE yields 4.9E-324, the smallest *positive* number and not -1.7976931348623157E308, the actual smallest number. Java/JavaScript are the only languages I know where this is the case.

  9. ChrisW

    Said on April 18, 2008 :

    Years ago I found a bug in SQL Server. As I recall, if you save a double NAN into a table, and the column that contains that NAN is indexed, then attempts to SELECT that value out via that index will crash SQL Server. This happens even if you’re not using that value in the index anyway (e.g., the index is MyInt + MyDouble, and you select WHERE MyInt=7, and the row with 7 has a NAN, then SQL Server will crash)

    I reported this in SQL 7, it remained in SQL 2000. Dunno if it’s been fixed yet.

  10. carsten

    Said on April 18, 2008 :

    Apart from the mathematical reason which has already been pointed out, it is very handy to use for variablres where the value is currently undefined; e.g. very handy to get started when computing min or max from a collection. You just have to use it right, ie use isNaN instead of ==.

    Matter of fact, you can use ==. x==x is the same as !Double.isNaN(x) since all numnbers which are not NaN are euqal to them selves. But this is clearly just a smart-ass hack. Use Double.isNaN() in real live.

  11. km

    Said on June 8, 2008 :

    aalmada, why should Math.Sin(0.0) return a NaN ? Isn’t a sine of 0 just a 0 ?

  12. aalmada

    Said on June 9, 2008 :

    @km

    Yes, you’re right… :-/

  13. james

    Said on October 4, 2008 :

    What about infinity? If x = double.infinity. Then shouldn’t x!=x as well?

  14. Zohrab Broyan

    Said on December 23, 2008 :

    To compare double equality you can use Equals() method instead of == operator.

  15. Dean

    Said on January 26, 2009 :

    “My question is why double.Nan exist? It only creates confusion…”

    How do you suppose you’d assign a NaN value to double without double.NaN?

  16. Johnny Pimpledick

    Said on July 14, 2010 :

    Thank you Mark R. for making mince meat outta these fools.
    You couldn’t have stated the obvious more clearly. Even a moron should get it now.

  17. Adnan M

    Said on June 7, 2012 :

    “My question is why double.Nan exist? It only creates confusion…”

    The same as “Why NULL exist in Database?”

  18. avc

    Said on November 21, 2013 :

    I am assigning NaN to values sometimes, forexample to indicate if the variable changed….

Post a Comment