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).]
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
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:
if NaN is = Float.intBitsToFloat(0x7fc00000) then x = Float.intBitsToFloat(0x7fc00000) y = Float.intBitsToFloat(0x7fc00000) x == y Where is my logic wrong?
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
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.
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
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.
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.