aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding NaN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Understanding NaN" Watch "Understanding NaN" New topic
Author

Understanding NaN

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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: 18745
    
  40

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: 18745
    
  40

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: 627
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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: 627
Thanks for the replies guys.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks for letting us know. I am actually preparing on SCJP 1.5 exam
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding NaN