Floating Point Math Errors

Mechanism for revealing floating-point math errors.

The functions declared in the math.h header file are not fully compliant with the ISO/IEC C standard's specifications for reporting errors. The standard requires floating point functions to set the errno variable to report an error condition. This mechanism is inefficient and un-informative.

EWL provides fpclassify() to simplify error checking. To check the validity of a computation returned by a function in math.h, call fpclassify() with the math function's result.

The following two listings compare these two error-checking approaches.

Listing: Using errno for Error Checking

#include <math.h>

#include <errno.h>

#include <stdio.h>

int main(void)

{

double x;

errno = 0;

x = log(0);

if (errno)

puts("error");

return 0;

}
Listing: Using fpclassify() for Error Checking

#include <math.h>

#include <stdio.h>

int main(void)

{

double x;

x = log(0);

if (fpclassify(x) != FP_NORMAL)

puts("error");

return 0;

}