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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Dan's exam question - Double.isNaN

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

The above prints true,true,true
What exactly is the method m doing? And why is isNaN() taking an argument? Can someone please explain?
Thanks
Sharda

Ranch Hand
Posts: 47
• Number of slices to send:
Optional 'thank-you' note:
Hai Sharda Vajjhala,
Double.isNaN is taking an argument to check whether the argument passed is NaN or not, returns accordingly.
We will have to analyse only one statement, that is... return (v != v == Double.isNaN(v)).
We start analysing from d3 to d1. d3 has the max double value. The expression goes always from left to right. So (v != v), obviously returns false, now (false == Double.isNaN(v)), v is max value of double, so isNaN returns false. Now the expression is like (false == false), returns true thats why.
As far as d2 is concerned, the above said will suit for this also.
d1 has NaN value. (v != v) RETURNS true !! Because two NaN values are always not equal. Now (true == Double.isNaN(v)), v has NaN value, so (true == true), return true.

Poorna

Sharda Vajjhala
Ranch Hand
Posts: 57
• Number of slices to send:
Optional 'thank-you' note:
Thanks
Sharda

Ranch Hand
Posts: 1865
• Number of slices to send:
Optional 'thank-you' note:
There isn't very much that I can add to Poornachandran's reply but the actual source code for the Double.isNaN method might be interesting.

The above source code demonstrates why the expression (v != v == Double.isNaN(v)) always returns true.

 What's that smell? I think this tiny ad may have stepped in something. the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising