| Author |
Understanding NaN
|
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Hi All, What does Float.NaN mean in Float f1 = new Float(Float.NaN)? and also, in the below code, why is -0.0f == 0.0f true? Thanks.
|
Be Humble... Be Nice.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
The reason 0.0f == -0.0f is because IEEE defines it as so. The reason the float class doesn't adhere to IEEE is because it wants 0.0f and -0.0f to be used as keys in a hashmap/set. (The JavaDoc clearly states this deviation from the IEEE specification) Henry [ September 07, 2008: Message edited by: Henry Wong ]
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
NaN means "not a number". It is a valid value defined by the IEEE specification. And can be achieve with certain calculations. For example, the square root of a negative number is a NaN. Zero divided by zero results in NaN. And Float.NaN is simply a static variable of the Float class that holds this value. This is so that you can compare results to see if it is NaN, without having to calculate it yourself. Henry [ September 07, 2008: Message edited by: Henry Wong ]
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
So, -1.2f == 1.2f is only for floats and doubles right and also, say, Float.NaN == Float.NaN is false because we can not compare two values which are undefined or unknown maybe right? Thanks.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
No, -1.2f == 1.2f is ofcourse false. Those are two different values. There is only one value for zero. There are (mathematically) not two numbers "negative zero" and "positive zero". NaN == NaN is false, because it is defined that way.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Lok Manick
Ranch Hand
Joined: Jan 24, 2005
Posts: 71
|
|
NaN can be related to infinity in maths ( i cannot find the symbol in my keyboard ) infinity is always != infinity |||ly NaN != NaN
|
with regards,
Loks (SCJP, SCWCD)
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
|
Thanks for the replies guys.
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8717
|
|
Hi Guys, This discussion applies only to the 1.4 exam - when the Math class got removed from the objectives so did NaN. hth, Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
|
Thanks for letting us know. I am actually preparing on SCJP 1.5 exam
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
|
Whooa!!! I didn't knew that Math class is not in the objectives. I was wondering why the book doesn't cover Math class.....
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
 |
|
|
subject: Understanding NaN
|
|
|