Re: [C] Returning a "failed" status from a function
Note that while floating point exceptions are typically disabled by GCC on GNU/Linux platforms, the same may not be true for other targets (and some installations of GNU/Linux may enable them).
To avoid generating floating point exceptions (SIGFPE), I would recommend you test for negative values before taking the square, but still use NAN as a flag for the error condition:
Code:
double sqrt_function(double a, double b) {
if (a < b)
return NAN;
else
return sqrt(a - b);
}
To enable the NAN macro, you may have to define the following constant before including <math.h>.
#define __USE_ISOC99
"We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson
Bookmarks