wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NaN in float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NaN in float" Watch "NaN in float" New topic
Author

NaN in float

Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61

I found following question in whizlab and have a doubt related this

Here can someone explain me what is this NaN .if I print value of f then it will display NaN.what is its meaning and why line number 3 comes out to be true while other are false .Also in whizlab it was written that all comparison related to NaN always results in false except for "!=" operator.It was said that != will always result in true and all other ==,<=,>= results in false.but when i run this program == comes out to be true . Is this NaN is present in all wrapper class.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Abhishek KumarSoni wrote:Here can someone explain me what is this NaN .

It's a constant defined in Float class. Similarly there is Double.NaN.
Abhishek KumarSoni wrote:if i print here calue of f then it will display NaN. what is its meaning

NaN means Not A Number. This value represents something that cannot be represented. You can get this value in various ways - 0.0/0.0, sqrt(-1), among others.
Abhishek KumarSoni wrote:and why st line number 3 output comes out to be false .

As already noted, this value can arise as a result of various operation. And it really represents an undefined or unrepresentable value, it doesn't make sense to compare it with any value. Even with itself. 2 NaNs are not equal.
Abhishek KumarSoni wrote:and Is this NaN is present in all wrapper class.

Only Float and Double classes.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Please don't edit your answer after you've posted it. Not at least after 10 minutes. It makes the following answer absurd, like it did just now.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

When you assign `Float.NaN to a Float reference, the value is auto-boxed to create a Float type object. Now, you've 2 Float reference there, point to the same Float.NaN. When you compare them, you're comparing the reference value of those Float type references. Since there is only single object, their reference value is same. Note, it is not really comparing Float.NaN, but the reference value. However, if you compare two different Float.NaN, you'll see it prints false, as in this code:


However, if you change the type of f3 and f4 to float, then you'll get false value.
Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61

R. Jain wrote:Please don't edit your answer after you've posted it. Not at least after 10 minutes. It makes the following answer absurd, like it did just now.

I will take care for it in furure, sir and thanks a lot for your response to my query.But actually in whiz lab output at line number was printed as false and reason being given was any comparison related to NaN results in false.except != which always results in true.so it means explanation was wrong at whizlab?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NaN in float
 
Similar Threads
The Same Doubt Again
Query on NaN Number
Please explain the concept of Float.Nan
Invoking equals method on float object
equals method behavior of Float