Wiki Markup |
---|
According to the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\], [§4.2.3, "Floating-Point Types, Formats, and Values"|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3] \[[JLS 2005|AA. Bibliography#JLS 05]\]: |
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
.
Because this unordered property is often unexpected, direct comparisons with NaN
must not be performed. Problems can arise when programmers write code that compares floating-point values without considering the semantics of NaN
. For example, input validation checks that fail to consider the possibility of a NaN
value as input can produce unexpected results. See rule NUM08-J . Check floating-point inputs for exceptional values for additional information.
Noncompliant Code Example
...
Automated detection of floating-point comparison operators is straightforward. Sound determination of whether the possibility of an unordered result has been correctly handled is not feasible in the general case. Heuristic checks could be useful.
FindBugs checks for the specific case of comparison with a constant NaN
.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="66797b7a8fd55a4d-54bd9d5d-41ec415e-960495c5-960e55ba2db39c3993222ced"><ac:plain-text-body><![CDATA[ | [[FindBugs 2008 | AA. Bibliography#FindBugs 08]] | FE: Doomed test for equality to NaN | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e55ec97d278bc4dd-c42de25e-42624d9c-be27a349-4a796b5cb9882b50694761ba"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§4.2.3, " Floating-Point Types, Formats, and Values" | http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3] | ]]></ac:plain-text-body></ac:structured-macro> |
...