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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# why Float.NaN and Double.NaN?

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Please have look on fallowing two lines,
float f=(float)Math.sqrt(-4);
System.out.println(f==Float.NaN);
Result is false.Then, what is the need of defining NaN fields in Float and Double wrapper classes.?
OR What is NaN..?
----------
Prashant

Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:
Hi,
In floating point comparison, if either operand is NaN, then the comparison is false.

Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:
hi,
All boolean operations involving "NaN" results in a false value.
Note:
Double.NaN > 1.0 -> false
Double.NaN < 1.0 -> false
Double.NaN == 1.0 -> false
Float.NaN < -3.0 -> false
Float.NaN > Float.POSITIVE_INFINITY -> false
Float.NaN < Float.POSITIVE_INFINITY -> false
(0.0 / 0.0) == (0.0 / 0.0) -> false
Regards,

Nayanjyoti Talukdar
Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I think that's not for all boolean operations.
As for example...

Double d = new Double(Double.NaN);
Double d1 = new Double(Double.NaN);
d.equals(d1) -> true. Here also NaN is involved.
Regards
Nayan.

girish rateshwar
Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Le me explain......
Consider this..
2 instances of class Double, d1 and d2.
The value of d1.equals(d2) is true if and only if
--->> d1.doubleValue() == d2.doubleValue() <<--
The result is true if and only if the argument is not null and is a Double object that represents a double that has the identical bit pattern to the bit pattern of the double represented by the object.
However, there are two exceptions:
--NOTE:*If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.<<----

*If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.
And the above two exceptions clearly indicate as to y u r d1.equals(d2) returns TRUE.

Regards,

[ December 16, 2002: Message edited by: girish rateshwar ]
[ December 16, 2002: Message edited by: girish rateshwar ]

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
All boolean operations involving "NaN" results in a false value.
"Boolean operations" is too general here. The statement would be true for all equality and comparison operators (==, <, >, <=, >=) except for the != operator, which always returns true if either operand is NaN. There are also various methods (which might be considered "operations") such as equals() (already noted) and isNaN() which can return true. So be careful about generalizing like this...

girish rateshwar
Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:
Hi jim,
I guess the statement is way too abstract, sorry bout it.
Regards,

 Consider Paul's rocket mass heater.