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

# Float.NaN == Float.NaN

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Why is this false?

If NaN equates to any value, it should equate to the same every time and then shouldn't I be able to test it for equality?
[This message has been edited by Dominic Mack (edited October 03, 2001).]

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hey Dominic,
I think you've got it backwards...Float.NAN doesn't equate to ANYTHING, even itself. In fact if you have float x = Float.NAN, this is the only time that the code (x != x) will return true.
Matt

Author and all-around good cowpoke
Posts: 13078
6
• Number of slices to send:
Optional 'thank-you' note:
That behavior is just build into the floating point math library - whenever that value appears it is treated as a special case, not a normal float operation.
Use the static method isNaN in the Float class to detect the special value.
Bill
------------------
author of:

Dominic Mack
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
if NaN is = Float.intBitsToFloat(0x7fc00000)
then
x = Float.intBitsToFloat(0x7fc00000)
y = Float.intBitsToFloat(0x7fc00000)
x == y
Where is my logic wrong?

William Brogden
Author and all-around good cowpoke
Posts: 13078
6
• Number of slices to send:
Optional 'thank-you' note:
Read my post again:
"That behavior is just build into the floating point math library - whenever that value appears it is treated as a special case, not a normal float operation."
When the floating point math library looks at a supposed float primitive, it can recognize several special case values that get special treatment. If x and y in your example are of type float then they get processed by the floating point library.
The class homework assignment is to identify the other special cases.
Bill

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:
According to the JLS:

All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

If this doesn't make sense to you then I would talk to the IEEE.

Thomas Paul
mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Dominic Mack:
if NaN is = Float.intBitsToFloat(0x7fc00000)
then
x = Float.intBitsToFloat(0x7fc00000)
y = Float.intBitsToFloat(0x7fc00000)
x == y
Where is my logic wrong?

x != y
Try it and you will see that x == y returns false.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums

Dominic Mack
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Thanks for all the responses...
I have tried the code and I know Nan != NaN... I was just wondering why. What I didn't understand was how it recognized this value (whatever it is) could not be equal to itself.
Bill, I guess I knew what you were saying, I just didn't understand it. Upon reading your second post, it made sense.

Ranch Hand
Posts: 1365
• Number of slices to send:
Optional 'thank-you' note:
Alright, let's brake out some math.
10/0 -> infinity -> NaN
1 + (10/0) -> infinity -> NaN
-10/0 -> negitive infinity -> NaN
0^0 -> indeterminate -> NaN
0/0 -> indeterminate -> NaN
The examples go on. Are any of these values equal to each other? No. That's why NaN != NaN.

 Don't get me started about those stupid light bulbs.