According to The Java Language Specification (JLS), §4.2.3, "Floating-Point Types, Formats, and Values" [JLS 2015]:"
NaN(not-a-number) is unordered, so the numerical comparison operators<,<=,>, and>=returnfalseif either or both operands areNaN. The equality operator==returnsfalseif either operand isNaN, and the inequality operator!=returnstrueif either operand isNaN.
...
Problems can ensue when the programmer uses such operators on NaN values in comparison operations. There is also a possibility that the input validation condition does not expect a NaN value as input.
Non-compliant Code Example
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 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.A frequently encountered mistake is the doomed comparison with NaN, typically in expressions. As per its semantics, no value can be compared to NaN using common operators, including NaN itself. This non-compliant example demonstrates one of such cases.
| Code Block | ||
|---|---|---|
| ||
public class NaNComparison { public static void main(String[] args) { double x = 0.0; double result = Double.NaN;Math.cos(1/x); // Returns NaN if input is infinity if (result == Double.NaN) { // Comparison is always false! System.out.println("Bothresult areis equalNaN"); } } } |
Compliant Solution
This compliant solution uses the method Double.isNaN() to check if 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 = Double.NaN;Math.cos(1/x); // Returns NaN when input is infinity if (Double.isNaN(result)) { System.out.println("Bothresult areis equalNaN"); } } } |
References
Risk Assessment
Comparisons with NaN values can lead to unexpected results.
Rule | Severity | Likelihood | Detectable | Repairable | Priority | Level |
|---|---|---|---|---|---|---|
NUM07-J | Low | Probable | Yes | Yes | P6 | L2 |
Automated Detection
Automated detection of comparison 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 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Klocwork |
| JAVA.COMPARE.NAN | |||||||
| Parasoft Jtest |
| CERT.NUM07.NAN | Avoid comparisons to Double.NaN or Float.NaN | ||||||
| PVS-Studio |
| V6038 |
Bibliography
...
...
Findbugs FE: Doomed test for equality to NaN