30

What's the difference between NaN and Infinity? When does NaN appear? What is it?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
Levi H
  • 3,426
  • 7
  • 30
  • 43
  • 1
    FYI, the title sort-of alludes that you think there's a difference between NaN-handling for floats and doubles. (At least that's what I thought) That would be interesting if you observed a difference, but you're just asking about NaN/Inf in general, which is sort-of pretty basic stuff. – Macke Nov 13 '11 at 20:23
  • Possible duplicate of [In Java, what does NaN mean?](https://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean) – phuclv Jul 28 '17 at 10:10

6 Answers6

29

From Wikipedia :

In computing, NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities.

And from MSDN :

  • Represents a value that is not a number (NaN). This field is constant.

  • The value of this constant is the result of dividing zero by zero.

  • This constant is returned when the result of an operation is undefined.

  • Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN.

Where as Infinity (positive infinity and negative infinity) is the result of a floating point operation that causes an overflow (For example 3.0 / 0).

Community
  • 1
  • 1
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • Infinity is not the result of divide-by-zero. It is mathematically incorrect (I would hope the processor wouldn't give that result, too). If C# does that, then boo to them. – Merlyn Morgan-Graham Nov 13 '11 at 15:36
  • 6
    @MerlynMorgan-Graham I won't argue with you about the 'mathematically' part but computationally I am correct since checking `Double.IsInfinity(3.0 / 0);` evaluates to true. Can you point out how exactly I may be wrong ? I'd like to know more :) – Nasreddine Nov 13 '11 at 15:41
  • 1
    If you checked it, I believe you. So see my last sentence and boo to MS :) – Merlyn Morgan-Graham Nov 13 '11 at 15:49
  • 4
    It's not just MS, http://codepad.org/jFlQi5pQ for C++ and it's true in Javascript as well. I think it's part of the floating point standard? – Chris Burt-Brown Nov 13 '11 at 16:01
  • @MerlynMorgan-Graham It *is* part of [the floating point standard](https://en.wikipedia.org/wiki/IEEE_floating_point) and there's nothing mathematically incorrect about it! Similar approach is used [in calculus](https://en.wikipedia.org/wiki/Division_by_zero#Extended_real_line) and [complex analysis](https://en.wikipedia.org/wiki/Riemann_sphere). – m93a May 07 '17 at 10:54
  • @m93a you misremember calculus or your math teacher sucked. The result of discrete division is not the same as the result of division by a variable x, when X approaches a given value. 3 / 0 is undefined, not infinity. 0 times infinity does not equal 3. Think about it... – Merlyn Morgan-Graham May 10 '17 at 16:09
  • @m93a but if they inherited the floating point standard's invalid definition, the I assume did it because that's at least useful sometimes. Floating point arithmetic necessarily makes compromises on reality, of course. So maybe yay to MS after all :) – Merlyn Morgan-Graham May 10 '17 at 16:12
  • @MerlynMorgan-Graham Did you even look at the links? – m93a May 11 '17 at 13:45
  • @m93a Looked at the links now. Didn't before because I was on a phone. The floating point standard defines it. Calculus doesn't. Your links and my comments have been in 100% agreement since my last comment. – Merlyn Morgan-Graham May 13 '17 at 01:02
  • @MerlynMorgan-Graham I feel like spamming but I just *had* to write this. Citation from the second link: "The extended complex numbers are useful in complex analysis because they allow for division by zero in some circumstances, in a way that makes expressions such as 1/0 = ∞ well-behaved." How is this **not** in contradiction with your statement that `3 / 0 is undefined, not infinity`? Now let's stop it! – m93a May 14 '17 at 09:09
  • 1
    @m93a Correction taken. I still think that the more frequent use case would have been NaN (though if Infinity loses less information in the less common use cases, and still supports typical error handling cases, maybe it was a reasonable decision). My original comment was attempting to demonstrate the difference for the OP succinctly, and unfortunately didn't have reality to back it up :) – Merlyn Morgan-Graham May 16 '17 at 05:55
14
  • Infinity is a mathematical construct:

    For instance, In euclidean space, the division through the null-element (zero in that case) should yield Infinity:

    1 / 0 = Infinity
    
  • Not a Number or NaN is a computational construct, that came along with parsers and programmatic limitations, and its output can be assigned different meaning depending on the function in question.

    For instance, a result may only be mathematically tractable using a different number system, which is easy for a mathematician to do, but in your function you may be left as the only pragmatic option to return NaN. Consider, the square root of -1:

    sqrt(-1) = NaN
    

    ...an operation which is easily tractable in complex and phase space.

Experiment:

Open up the JavaScript.Console (CTRL+SHIFT+J) in your browser, and type

>>> Math.sqrt(-1)
NaN

>>> 1/0
Infinity

>>> Number.MAX_VALUE
1.7976931348623157e+308

>>> Number.MAX_VALUE *2
Infinity

>>> parseFloat("I am not a Number")
NaN

In C# the typical 'NaN-situations' are mostly handled through Exceptions:

csharp> Int64.MaxValue;
9223372036854775807
csharp> Int64 i_64 = Int64.MaxValue;
//the number will overflow into the sign-bit
csharp> i_64 +=1;
//...or similarly with Doubles...
csharp> Double.MaxValue;
1.79769313486232E+308

//following, an exception is thrown before overflowing
csharp> Int64 i_64 = Int64.MaxValue+1;
{interactive}(1,29): error CS0220: The operation overflows at compile time in ch
ecked mode

Dynamic typed languages:

Overall, the usage of NaN is somewhat flexibly assigned in different programming languages. Using NaN at the loss of some 'contextual information', is convenient in dynamically typed scripting languages, where programmers generally do not want to bother with complex exception-types and handling thereof.

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
5

Usually happens when you divide 0 by 0. Read more here: http://msdn.microsoft.com/en-us/library/system.double.nan.aspx

misha
  • 2,760
  • 3
  • 28
  • 30
3

check whether double has value, if not then return 0

if (double.Equals(double.NaN, myValue))
    myValue= 0;
Ori Dar
  • 18,687
  • 5
  • 58
  • 72
Jack
  • 31
  • 2
1

NaN stands for "Not a number Value". To avoid exceptions you can use IsNaN to determine wether a value is not a number.

Kolt
  • 395
  • 1
  • 4
  • 10
1

NaN means "Not a number" and tells you that this variable of type double hasn't any value.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56