3

I'm parsing the "NaN" value in to double and it is giving me as "NaN".

double  d = Double.parseDouble("NaN");
System.out.println(d);

It is printing the "NaN" value. I am surprised because I would expect that parsing a non-number for a double would result in NumberFormatException in Java.

Can anyone explain why the parse succeeded, and what was really stored in the variable?

Community
  • 1
  • 1
Yogesh
  • 715
  • 1
  • 7
  • 20
  • 4
    What would you expect it to be? – NilsH Apr 29 '13 at 10:53
  • 2
    I think that this is a reasonable Java question, not a very poor Javascript question. Edited accordingly. – Jirka Hanika Apr 29 '13 at 12:38
  • 2
    It's sad that this was closed - it has a single, unambiguous answer that hinges on the relevant Java and IEEE specs, and is extremely constructive if you've never seen this edge case before. What a way to discourage a new user. – candu Sep 22 '15 at 19:56

4 Answers4

10

You are probably surprised that this special value could have been stored in a double variable. This is because Java uses the IEEE 754 standard for floating point representation.

This standard allows to represent not just specific rational numbers, but also a few special values, like plus infinity or minus infinity, which are essentially very big numbers - results of calculations that yielded bigger results than any representable rational value. (There are also denormal numbers, lying somewhere between the finite and infinite values; precision of those is reduced compared to the precision you are used to, but not so extremely as with the infinities. The loss of precision is caused by giving more room to the exponent at the expense of the mantissa.)

However, a NaN is not an infinity, nor an indicator of an overflow or an underflow. It is not an inaccurate result of any computation. It is a non-value, similar to a NULL in the database. In fact, try this in your program:

System.out.println(d == d)

It will print out false.

Why false? Loosely speaking, if you do not know the values of two quantities, there is no reason to assume that they are the same value. They usually are not.

It is actually quite unusual to see a NaN on input to double.ParseDouble, except during some kind of unmarshalling.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • Perfect answer. Also, I was really happy to learn that `(d == d)` could actually return false ! Thanks... – Orabîg Apr 30 '13 at 07:28
2

NaN is not a number.

"NaN" stands for "not a number". "Nan" is produced if a 
floating point operation has some input parameters that cause the 
operation to produce some undefined result. For example, 0.0 divided 
by 0.0 is arithmetically undefined. Taking the square root of a negative 
number is also undefined. 
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

Well, "NaN" is "Not a Number", and a valid value for a double.

So, if you parse "NaN", it seems logical to get "NaN" as a result.

You should take a look at In Java, what does NaN mean?

Community
  • 1
  • 1
Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • Yes. it is straight forward explanation and I also know that. but you did not really understand my question. it is *what was really stored in the variable?* I mean so in this cas variable "d" will store "NaN" as string value or what ? – Yogesh Sep 27 '19 at 11:21
1

Because "NaN" is Not A Number.
Double.parseDouble() expects a double value in string format

Ajinkya
  • 22,324
  • 33
  • 110
  • 161