tags:

views:

139

answers:

3
+2  Q: 

check NaN number

Hi,

is it possible to check if a number is NaN or not?

UPDATE: my mistake. fixed the Question.

A: 

you are looking for null, but that is only useful for pointers. a number can't be null itself, it either has a known value that you put in there or random data from whatever was there in memory before.

Scott M.
+6  A: 

That would depend on what you meant by NILL. Normal integral types don't have a concept of nilliness, unless you mean zero, in which case you'd just use:

if (myInt == 0) { ... }

Floating point types, treat similarly if you mean zero, but they also have the concept of not-a-number (one of the possible results of a dubious calculation). You can either look for a function like isnan or just use the property that an NaN is not equal to any other number, including itself:

if (myFloat != myFloat) { ... }

Or, if you're talking about pointers of the NULL variety, that's also 0 in C++ so you can use:

if (myPtr == 0) { ... }
paxdiablo
And do wrap `myFloat != myFloat` into some sort of `isnan` function with a comment, lest future readers that don't know about it be very confused.
GMan
+2  A: 

Under Linux/gcc, there's isnan(double), conforming to BSD4.3.

C99 provides fpclassify(x) and isnan(x).
(But C++ standards/compilers don't necessarily include C99 functionality.)

There ought to be some way with std::numeric_limit<>... Checking...

Doh. I should have known... This question has been answered before... http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c http://stackoverflow.com/questions/235386/using-nan-in-c http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan

Mr.Ree