This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# NaN in float

Abhishek KumarSoni
Ranch Hand
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
Posts: 375
1
• 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
Posts: 375
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
Posts: 375
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
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?