When is a Number Not a Number?

only when Double.IsNaN(double d) returns true, [Update:] or when it is not equal to itself.

I guess this makes sense, just because two things aren’t numbers doesn’t necessarily mean they are equivalent.


if (double.NaN != double.NaN)
    Debug.Print("IEEE rules!");

2 comments so far

  1. Doug on

    From wikipedia which has a decent writeup on the NaN:

    “A NaN does not compare equal to any floating-point number or NaN, even if the latter has an identical representation. One can therefore test whether a variable has a NaN value by comparing it to itself (i.e. if x \neq x then x is NaN).”

    The behavior makes more sense when you consider the NaN to be more of an error case that is out of the domain of a double and therefore double comparison doesn’t apply.

  2. Kirk Kuykendall on

    Doug –

    Thanks for the feedback. Been working a bit with Dundas Chart, which uses NaN extensively. I see what you mean about using it for an error case … sure is cheaper than throwing an exception.

    Kirk


Leave a reply