Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Float.NaN == Float.NaN

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic