Pattern: Malformed test for NaN
Issue: -
System.Double.NaN
, which represents a value that's not a number, results when an arithmetic operation is undefined. Any expression that tests for equality between a value and System.Double.NaN
always returns false
. Any expression that tests for inequality (!=
in C#) between a value and System.Double.NaN
always returns true
.
Example of incorrect code:
Console.WriteLine(0 / zero == double.NaN);
Example of correct code:
Console.WriteLine(double.IsNaN(0 / zero));