How can I compare a float to NaN if comparisons to NaN always return false?

Solution 1:

You want float.IsNaN(...). Comparisons to NaN always return false, no matter what the value of the float is. It's one of the quirks of floating points.

That means you can do this:

if (f1 != f1) { // This conditional will be true if f1 is NaN.

In fact, that's exactly how IsNaN() works.

Solution 2:

Try this:

if (float.IsNaN(fValue))
{
}