This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NaN values.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NaN values.." Watch "NaN values.." New topic
Author

NaN values..

Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Given the code:
Float f1 = new Float(Float.NaN);
Float f2 = new Float(Float.NaN);
System.out.println( ""+ (f1 == f2)+" "+f1.equals(f2)+ " "+(Float.NaN == Float.NaN) );

Ans : false,true,false

Although f1.equals(f2) should return false, it returns true. Javadoc gives two exceptional cases:
. If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
. If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

Doubt: IS it true for Double.NaN values also???


SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Swapnil Trivedi:
...Javadoc gives two exceptional cases:
. If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
. If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

Doubt: IS it true for Double.NaN values also???

Well, the API documentation for Double contains the same wording. I wonder how difficult this would be to test?
[ June 25, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
I just got that in a practice exam and I posted it here....
My apologies



Regards
Swapnil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NaN values..
 
Similar Threads
equals()
Simple question
float equality
Invoking equals method on float object
NaN