# Thread: How does one catch NaN type errors?

1. ## How does one catch NaN type errors?

Is there a good way to catch all the NaN type errors like:
• 1.#IND
• -1.#IND
• -1.#INF
• 1.#INF
• etc....

I can't do:
Code:
if (var == -1.#IND)
because the # calls up compiler errors.

Anyhow, i'm looking for a suggestion for a catchall function for numerical explosion errors that returns 0 if it's a normal double and 1 otherwise. Hope you can help!

Thanks!

2. Somewhere in math.h (or is it float.h - check your compiler), there is a function called isNan(), which you pass your suspect result to, and it returns true or false.

3. Someone just wrote me that those functions are a C99 nonstandard function. I have no clue how to enable those. Any simple way to code these functions?

4. Code:
float var = 1.0/0.0;
if ( var != var )
should work.
Kurt

5. Doesn't work. The compiler won't let me divide by zero.

6. Really I'm looking for the finite function. That way I can get a catch-all

7. > those functions are a C99 nonstandard function.
Odd, I thought they were a C99 standard function.

There are two C standards, C89 which everyone should be able to do and C99 which only a few compilers are edging towards full compliance.

But anyway, many compilers have all sorts of extensions for all sorts of things, so you may still have something you can use.

I've no idea what ZuK was up to with a divide by zero?

8. He wanted a NaN.

9. It seems to me the OP wants isnormal() to exclude all the NaN's, indefinites and infinities.

10. Perhaps:
Code:
if (var != numeric_limits<double>::quiet_NaN())
http://www.dinkumware.com/manuals/?m...its::quiet_NaN

11. Originally Posted by Salem
It seems to me the OP wants isnormal() to exclude all the NaN's, indefinites and infinities.
Correct! That's what I want

12. The compiler is smart enough to refuse to compile my original example
Code:
#include <iostream>

int main() {
float f1 = 0;
float f2 = f1/0;
if ( f2 != f2 ) {
std::cout << "error dividing. " << f2 << std::endl;
}
}
my output
Code:
dz.cc: In function `int main()':
dz.cc:5: Warnung: division by zero in `f1 / 0'
kurt@power:~\$ ./a.out
error dividing. nan
Kurt

13. Ah, but taking in user input is a good way to get bad numbers... those users and their wacky ways.

14. The compiler is smart enough to refuse to compile my original example
Hmm... it's somewhat debatable if that is a good thing, unless you have your compiler set to stop on warnings.

http://www.jdl.co.uk/briefings/divByZeroInCpp.html

15. Did a little experimenting now.
comparing a var with itself compares false for nan only.
inf compares true.
@Mario F.: you are right it's just a warning.
Kurt