LibC comes with a default math error handler which prints any errors to the console. These messages are hard coded in English and cannot be localized. The math functions call this handler unless you write your own and register it in your start up or early main code.
To register your own math handler (called mymatherrors in this example), add the following line to your start up code or early in your main function.
err=matherr_handler(mymatherrors);
This function has only one possible error: ENOCONTEXT. This is returned only if you call this function at interrupt time or your thread does not have LibC context. Most versions of LibC are able to build context for the calling thread if it didn’t have any to start with.
For a silent math error handler, code mymatherrors as follows:
int mymatherrors
(
__FP_EXCEPTION *exc
)
{
return exc->retval;
}
For form, the function should at least return the value of the exception. You can modify this value which, in most cases, causes the math function encountering an error to return your value.
If your application expects to receive math errors and needs to handle each type differently, mymatherrors can switch on the value of what type of error occurred. For example:
switch (exc->type)
{
default :
break;
case DOMAIN :
msg = "Domain error";
break;
case SING :
msg = "Argument singularity";
break;
case OVERFLOW :
msg = "Overflow range error";
break;
case UNDERFLOW :
msg = "Underflow range error";
break;
case TLOSS :
msg = "Total loss of significance";
break;
case PLOSS :
msg = "Partial loss of significance";
break;
}
These error messages are only an example of what LibC does in its math-error handler.
To print error messages, you can use any function from fprintf, consoleprintf, to OutputToScreen. For example:
#define ERR_PROMPT "%s: %s in %s\n"
char name[80];
fprintf(stderr, ERR_PROMPT, getnlmname(getnlmhandle(),
name), msg, exc->name);
Sample output would be the following:
mynlm: Underflow range error in pow