If we want to use a mathematical function, such as the inverse of the string function asin (x), if the value of the variable x is provided by the user or an intermediate result, then the call must be judged to be a reasonable range of values, whether full |x|<=1? That
if(fabs(x)<=1)
y=asin(x);
else
y=…
Logarithmic functions can also be handled similarly. However, if the Power function pow (x,y) is encountered, the problem is not so simple. Careful analysis will find:
Y X |
Negative decimal |
Negative integer |
0 |
Integer |
Decimal |
Negative decimal |
No meaning |
Make sense |
Make sense |
Make sense |
No meaning |
Negative integer |
No meaning |
Make sense |
Make sense |
Make sense |
No meaning |
0 |
No meaning |
No meaning |
Make sense |
Make sense |
Make sense |
Integer |
Make sense |
Make sense |
Make sense |
Make sense |
Make sense |
Decimal |
Make sense |
Make sense |
Make sense |
Make sense |
Make sense |
For example: Pow ( -1.2,-1.2) =-1. #IND. If you want to process it programmatically, you need at least six if statements. Even so, there is the trouble: how to determine the value of a double variable is an integer or a decimal?
In order to deal with the anomalies appearing in mathematical function operations, VC + + provides a function _mather, whose prototype is in <math.h>:
int _matherr( struct _exception *except );
In order to take advantage of this function, you simply define one such function where the mathematical function is applied, such as
#include <math.h>
#include <stdio.h>
void Main ()
{
double x,y,z;
x=-1.23;
Y=-1;
Z=pow (x,y);
printf ("%g\n", z);
y=-1.1;
Z=pow (x,y);
printf ("%g\n", z);
}
int _matherr (struct _exception *except)
{char* errorstring[] = {"_domain", "_sing"
, "_overflow", "_ploss", c16/> "_tloss", "_underflow"};
printf ("Error function name is%s\n", except->name);
printf ("The Varianbles arg1=%g,arg2=%g\n", except->arg1,except->arg2);
printf ("The Error type =%s\n", Errorstring[except->type]);
printf ("The Error value=%g\n", except->retval);
except->retval=1234;
printf ("After handling error value=%g\n", except->retval);
return 1;
}