"NaN" in Java

techbro
techbro used Ask the Experts™
on
I am taking a java mock exam in my exam simulator - enthuware. I am studying for a certification exam. I have approached this problem about "NaN", which I am not familiar with.

The Question is:

Which of the following statements about NaNs are true ?
A. Float.NaN == Float.NaN
B. Float.NaN == Double.NaN
C. Float.NaN >= 0
D. Float.NaN < 0
E. None of the above.

Here, Option E is the correct answer according to the exam simulator.

Since NaN means "Not a Number", it is obvious that C and D are incorrect. But I need to know why option A is not correct in this problem?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:

From this site:
http://www.concentric.net/~ttwang/tech/javafloat.htm

All boolean operations involving "NaN" results in a false value.

That is just convention as I understand
Awarded 2011
Awarded 2011

Commented:

More examples from the same site.
No direct exmaple like yours Float.NaN == Float.NaN
 - but definitiona bove requires that it wouyld be false

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

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Also, http://en.wikipedia.org/wiki/NaN   NaN = "Not a Number".  If it is "Not a Number", then it can't be a float which is a number.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Awarded 2011
Awarded 2011
Commented:
What you mentyioned, is certainly true,  but it still does not explain why it is not equal to itself, like in
Float.NaN == Float.NaN

I think it is just by definition
Float.NaN is practically no one knows what (we only know what it is not, which is not very much),
so they stated that all operations involving
Float.NaN  should by definition always return false.
Float.NaN can be produced as 0.0f/0.0f or as Math.sqrt(-2.0f)
Are those two equal ? By definition - no. and intuitively seems it is a right definition.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I would say that since it is 'undefined', it can't be equal to or compared to anything.  There is nothing valid to support the comparison.
Awarded 2011
Awarded 2011

Commented:
Sure, that seems also quite reasonable explanation.
Mick BarryJava Developer
Top Expert 2010

Commented:
The JLS defines NaN ss *unordered* so all comparison operators return false (you cannot compare something that has no order).
Mick BarryJava Developer
Top Expert 2010
Commented:
"NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (§15.21.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN."

http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#153654

For == it is following the IEEE 754 standard

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5198

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial