Catch Math Function exception

Source: Internet
Author: User
Tags asin integer pow printf

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.