programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Liutauras Vilda
Sheriffs:
• Tim Cooke
• Bear Bibeault
• paul wheaton
Saloon Keepers:
• Carey Brown
• Stephan van Hulst
• Tim Holloway
• Mikalai Zaikin
• Piet Souris
Bartenders:

# NaN

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
Can anyone explain to me why the answer of the following code is
False
True
1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
Thank you.

Ranch Hand
Posts: 234
• Number of slices to send:
Optional 'thank-you' note:
Can anyone explain to me why the answer of the following code is
False
True
1: Double a = new Double(Double.NaN); // a object
2: Double b = new Double(Double.NaN); // b object
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
// this prints false as the reference of a and b is different
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
// this prints true because equals checks for what is contained in the object. Both a and b contain NaN and hence
True
Note: NaN == Nan will display false

Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by sona nagee:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
// this prints false as the reference of a and b is different

9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
// this prints true because equals checks for what is contained in the object. Both a and b contain NaN and hence
True

I have a small problem with explanation to answer 1
The documentation specifies that Double.NaN is a static double (Note the primitive type)
So there is no question of object references when we are dealing with Double.NaN == Double.NaN
So why should it return FALSE??
Thanks
Anand

sona gold
Ranch Hand
Posts: 234
• Number of slices to send:
Optional 'thank-you' note:
I have a small problem with explanation to answer 1
The documentation specifies that Double.NaN is a static double (Note the primitive type)
So there is no question of object references when we are dealing with Double.NaN == Double.NaN
So why should it return FALSE??
sorry i missed this
even though we are creating wrapper objects we are comparing
static dobule Double.NaN
as i have stated in my earlier post also
NaN == NaN is false
both for float and for double
the reason being NaN does not defint one thing
anything that does not fit into a number is nt a number
hope it clears it

Ranch Hand
Posts: 195
• Number of slices to send:
Optional 'thank-you' note:
Example to follow up Sona's post.
Math.acos(43.5) == Double.NaN
Math.sqrt(-5) == Double.NaN
Obviously, Math.acos(43.5) != Math.sqrt(-5), therefore Double.NaN != Double.NaN.
Each instance of Double.NaN is a unique value.

anand raman
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
The best possible explanation for this predicament is in one of the threads on this forum which states
1. NaN are not oridnal whereas POSITIVE_INFINITY and NEGATIVE_INFINITY are ordinal.
Hope thie helps
-Anand

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
Susie,
NaN has the property that NaN != Nan. Simple as that. It's probably easier to think of it as "not determined". Making a comparison on two values that are undetermined will therefore return false.
The object reference comparisons will return true as you are checking if they are comparing the same objects in memory. This is true because Double objects are immutable and Java works out that it is referencing two identical objects. Being efficient it holds only one object in memory and points both references to it.
HTH
Paul

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:
Actually you have it backwards.
This is from the API:
Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if
f1.floatValue() == f2.floatValue()

also has the value true. However, there are two exceptions:
1) If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
2) 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.

 Legend has it that if you rub the right tiny ad, a genie comes out. Low Tech Laboratory https://www.kickstarter.com/projects/paulwheaton/low-tech-0