Wrong NaN comparison¶
ID: java/comparison-with-nanKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - correctnessQuery suites: - java-security-and-quality.qls
Click to see the query in the CodeQL repository
The special floating-point numberNaN is defined to be different from all other floating-point numbers, including itself, when compared using the equality operators,== and!=.
Recommendation¶
To check whether a variablex isNaN use the methodisNaN that is defined on bothjava.lang.Float andjava.lang.Double.
Example¶
The expressionx==Double.NaN is always false. This expression should be replaced byDouble.isNaN(x), which accurately identifies whetherx is equal toDouble.NaN.
References¶
Java Language Specification:Numerical Equality Operators == and !=.