home *** CD-ROM | disk | FTP | other *** search
- /*
- // MATHERR.C Sample error handling routine for extrinsic functions
- // in a ROM environment. This code should be customized
- // for a particular application and then replace the
- // matherr() function supplied by Microsoft in the
- // applicable run-time library.
- //
- // Copyright (C) 1988, 1989 Paradigm Systems. All rights reserved.
- //
- // The _matherr() function is provided to interface to matherr(). It is
- // the responsibility of matherr() to handle all exceptions since except
- // for setting errno, _matherr() has no other impact and will return to
- // the caller with the result to be used.
- //
- */
-
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <float.h>
-
-
- double _matherr(why, fun, arg1p, arg2p, retval)
- _mexcep why ;
- char *fun ;
- double *arg1p, *arg2p ;
- double retval ;
- {
- struct exception e ;
-
- /* Initialize the exception structure for the call to matherr() */
- e.type = why ;
- e.name = fun ;
- e.arg1 = *arg1p ;
- e.arg2 = *arg2p ;
- e.retval = retval ;
-
- /* Call matherr() and abort if it can't handle the exception */
- if (matherr(&e) == 0) {
- switch (why) {
- case DOMAIN:
- case SING:
- errno = EDOM ;
- break ;
-
- case OVERFLOW:
- case UNDERFLOW:
- case TLOSS:
- case PLOSS:
- errno = ERANGE ;
- break ;
- }
- }
-
- /* Return the value to be used */
- return e.retval ;
- }
-
-
- /*
- // This version of matherr() has been modified to return a HUGE_VAL that
- // can be held in type float. This is done to prevent the functions such
- // as exp() and log() from returning the default of HUGE_VAL which will
- // cause a floating point overflow exception when it is assigned to a float
- // variable.
- //
- // matherr() should be customized for the particular application. This
- // example show the general means to look for specific exception conditions
- // and handle them in a manner consistent with the application.
- //
- // This version will in some cases correct the fault, display an error
- // message and return.
- */
-
- int matherr(x)
- struct exception *x ;
- {
- int code = 0 ; /* 0 if matherr() can't handle the exception */
-
- switch (x->type) {
- case DOMAIN:
- if (stricmp(x->name, "exp") == 0 || strnicmp(x->name, "log", 3) == 0) {
- x->retval = FLT_MAX * (x->retval < 0.0 ? -1.0 : 1.0) ;
- code = 1 ;
- }
- else if (stricmp(x->name, "sqrt") == 0) {
- x->retval = sqrt(fabs(x->arg1)) ;
- code = 1 ;
- }
- break ;
-
- case SING:
- if (stricmp(x->name, "exp") == 0 || strnicmp(x->name, "log", 3) == 0) {
- x->retval = FLT_MAX * (x->retval < 0.0 ? -1.0 : 1.0) ;
- code = 1 ;
- }
- break ;
-
- case OVERFLOW:
- if (stricmp(x->name, "exp") == 0 || strnicmp(x->name, "log", 3) == 0) {
- x->retval = FLT_MAX * (x->retval < 0.0 ? -1.0 : 1.0) ;
- code = 1 ;
- }
- break ;
-
- case PLOSS:
- case TLOSS:
- case UNDERFLOW:
- default:
- break ;
- }
-
- /* Return the code showing the exception has been handled */
- return code ;
- }