#79 – Equality Checks for NaN

You should use the float and double static methods for checking for NaN values (E.g. float.isNaN), rather than using the equality operator.  The equality operator will not work to compare a value against double.NaN and float.NaN.

 // Easiest way--use static method
 bool b1 = double.IsNaN(0.0 / 0.0);     // true

 // == operator doesn't work
 bool b2 = (0.0 / 0.0) == double.NaN;   // false !

 // object.Equals does work
 bool b3 = object.Equals(0.0 / 0.0, double.NaN);
Advertisement