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!");
Advertisements

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: