Floating point adventures

Using == to compare floats and doubles is not only problematic when dealing with numerical precision, but also when dealing with NaNs: In C#, the equals-operator returns false when comparing NaNs. If NaNs shall compare equal (e.g. when suppressing PropertyChanged events, as I’m doing), use Double.CompareTo(double). The IComparable<> interface requires that A.CompareTo(A) always returns zero and therefore does correctly (for this case) work with NaNs and +/- infinity.

Leave a Reply