How does one catch NaN type errors?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-16-2006
testing123
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!
• 08-16-2006
Salem
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.
• 08-16-2006
testing123
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?
• 08-16-2006
ZuK
Code:

float var = 1.0/0.0;
if ( var != var )

should work.
Kurt
• 08-16-2006
testing123
Doesn't work. The compiler won't let me divide by zero.
• 08-16-2006
testing123
Really I'm looking for the finite function. That way I can get a catch-all
• 08-16-2006
Salem
> 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?
• 08-16-2006
CornedBee
He wanted a NaN.
• 08-16-2006
Salem
It seems to me the OP wants isnormal() to exclude all the NaN's, indefinites and infinities.
• 08-16-2006
Daved
Perhaps:
Code:

if (var != numeric_limits<double>::quiet_NaN())
http://www.dinkumware.com/manuals/?m...its::quiet_NaN
• 08-16-2006
testing123
Quote:

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
• 08-16-2006
ZuK
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
• 08-16-2006
Wraithan
Ah, but taking in user input is a good way to get bad numbers... those users and their wacky ways.
• 08-16-2006
Mario F.
Quote:

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
• 08-16-2006
ZuK
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last