According to The Java Language Specification (JLS), §4.2.3, "Floating-Point Types, Formats, and Values" [JLS 20052015]:
NaN
(not-a-number) is unordered, so the numerical comparison operators<
,<=
,>
, and>=
returnfalse
if either or both operands areNaN
. The equality operator==
returnsfalse
if either operand isNaN
, and the inequality operator!=
returnstrue
if either operand isNaN
.
...
This noncompliant code example attempts a direct comparison with NaN
. In accordance with the semantics of NaN
, all comparisons with NaN
yield false (with the exception of the !=
operator, which returns true). Consequently, this comparison always return false
, and the "result is NaN"
message is never printed.
...
[Seacord 2015] | NUM07-J. Do not attempt comparisons with NaN LiveLesson |
...