According to the _The Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\], [§4 (JLS), §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 2015]: Wiki Markup
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, problems 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 "(see NUM08-J. Check floating-point inputs for exceptional values" for additional information).
Noncompliant Code Example
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.
Code Block | ||
---|---|---|
| ||
public class NaNComparison { public static void main(String[] args) { double x = 0.0; double result = Math.cos(1/x); // returnsReturns NaN if input is infinity if (result == Double.NaN) { // comparisonComparison is always false! System.out.println("result is NaN"); } } } |
...
This compliant solution uses the method Double.isNaN()
to check whether the expression corresponds to a NaN
value.:
Code Block | ||
---|---|---|
| ||
public class NaNComparison { public static void main(String[] args) { double x = 0.0; double result = Math.cos(1/x); // returnsReturns NaN when input is infinity if (Double.isNaN(result)) { System.out.println("result is NaN"); } } } |
...
Comparisons with NaN
values can lead to unexpected results.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
NUM07-J |
Low |
Probable |
Medium | P4 | L3 |
Automated Detection
Automated detection of floating-point comparison operators with NaN
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.
Tool | Version | Checker | Description | |||||
---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.NUM07. |
...
NAN | Avoid comparisons to Double.NaN or Float.NaN | ||||||||
PVS-Studio |
| V6038 |
Bibliography
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="59a3140b-d5ca-4e1f-8795-88abcee1ccf7"><ac:plain-text-body><![CDATA[
[[FindBugs 2008
]]></ac:plain-text-body></ac:structured-macro>
[ |
[[JLS 2005
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]
]]></ac:plain-text-body></ac:structured-macro>
[Seacord 2015] |
...
NUM06-J. Use the strictfp modifier for floating point calculation consistency across platforms 03. Numeric Types and Operations (NUM)