matherr(3C)
_________________________________________________________________
matherr function
Determine if errors occurred during a math routine.
_________________________________________________________________
CALLING SEQUENCE
#include <math.h>
int matherr(), notdefault;
struct exception *ptrexception;
notdefault = matherr(ptrexception);
where notdefault is non-zero if the last error (errno)
will not be updated with the appropriate error code
(ERANGE or EDOM).
ptrexception is the address of the structure
exception that is passed to the matherr function.
The structure contains information to describe the
error and the default value to be returned to the
user (field retval). The default return value can
be modified by the matherr function; this value is
returned to the caller of the math function that has
an error.
DESCRIPTION
Various math library functions call the matherr function when
errors occur. Users can define their own matherr function to do
any special handling.
The structure that is pointed to by ptr_exception above is struct
exception. The exception structure, defined in math.h, is
defined as follows:
struct exception {
int type; /* type of error */
char *name; /* math function name */
double arg1; /* argument #1 */
double arg2; /* argument #2 */
double retval; /* return value */ };
The value for the type field is one of these values:
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
matherr(3C)
Name Value Meaning
DOMAIN 1 Domain error
SING 2 Singularity
OVERFLOW 3 Overflow in math routine
UNDERFLOW 4 Underflow in math routine
TLOSS 5 Total loss of significance
PLOSS 6 Partial loss of significance
These math routines call the matherr function:
Routine Error Code Error Type Default Cause
acos EDOM DOMAIN 0.0 |x| > 1.0
asin EDOM DOMAIN 0.0 |x| > 1.0
atan2 EDOM DOMAIN 0.0 y == 0.0
cos ERANGE PLOSS * Too large
ERANGE TLOSS * Too large
cosh ERANGE OVERFLOW HUGE Overflow
exp ERANGE OVERFLOW HUGE Overflow
ERANGE UNDERFLOW 0.0 Underflow
hypot ERANGE OVERFLOW HUGE Overflow
log EDOM SING -HUGE x == 0.0
EDOM DOMAIN -HUGE x < 0.0
log10 EDOM SING -HUGE x == 0.0
EDOM DOMAIN -HUGE x < 0.0
pow EDOM DOMAIN 0.0 x == 0 && y < 0
EDOM DOMAIN 0.0 x < 0 && y not int
ERANGE OVERFLOW +/-HUGE Overflow
ERANGE UNDERFLOW 0.0 Underflow
sin ERANGE PLOSS * Too large
ERANGE TLOSS * Too large
sinh ERANGE OVERFLOW +/-HUGE Overflow
sqrt EDOM DOMAIN 0.0 x < 0.0
tan ERANGE PLOSS * Too large
ERANGE TLOSS * Too large
* As much as possible of the return value is returned.
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
matherr(3C)
RETURNS
When an error occurs during a call to a math function, matherr
returns the default value contained in the retval field of the
structure exception unless you redefine the function.
RELATED FUNCTIONS
The function matherr has no related functions.
EXAMPLE
This example demonstrates how a user could define and use the
matherr function:
/* Program test for the matherr() function */
#include <math.h>
#include <stdio.h>
double result;
double inputs[3] = {1.0, 2.0, -1.0};
int i;
main() {
for (i = 0; i < 3; i++) {
result = sqrt(inputs[i]);
printf("sqrt(%4.1f) = %11.8f\n",
inputs[i], result);
}
return 0;
}
int matherr(ptr)
struct exception *ptr;
{
printf("Error in %s(%4.1f)\n",
ptr -> name, ptr -> arg1);
/* Change return value */
ptr -> retval = -1.0;
return 0;
}
When you call this program, the output produced is:
sqrt( 1.0) = 1.00000000
sqrt( 2.0) = 1.41421356
Error in sqrt(-1.0)
DOMAIN error in sqrt
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)
matherr(3C)
sqrt(-1.0) = -1.00000000
DG/UX 4.00 Page 4
Licensed material--property of copyright holder(s)