捕获数学函数异常

王朝vc·作者佚名  2006-01-17
窄屏简体版  字體: |||超大  

捕获数学函数异常

作者: 江汉石油学院计算机系 周云才

下载本文配套源代码

假如我们要用一个数学函数,比如反正弦函数asin(x),如果变元x的值是由用户提供或某个中间结果,则在调用时必须判断其取值范围是合理,是否满|x|<=1?即

if(fabs(x)<=1)

y=asin(x);

else

y=…

对数函数也可作类似的处理。但是如果遇到幂函数pow(x,y)时,问题就不那么简单了。仔细分析将发现:

y

x

负小数

负整数

0

整数

小数

负小数

无意义

有意义

有意义

有意义

无意义

负整数

无意义

有意义

有意义

有意义

无意义

0

无意义

无意义

有意义

有意义

有意义

整数

有意义

有意义

有意义

有意义

有意义

小数

有意义

有意义

有意义

有意义

有意义

例如:pow(-1.2,-1.2)=-1.#IND。如果要编程处理,至少需要六个if语句。即使如此,也有麻烦:如何判断一个double型的变元的值是整数还是小数?

为了处理数学函数运算中出现的异常,VC++提供了一个函数_mather,其原型在<math.h>中: int _matherr( struct _exception *except );

为了利用此函数,只需在应用数学函数的地方定义一个这样的函数,例如#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",

"_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;

}

编译、运行,结果为

-0.813008

Error function name is pow

The varianbles arg1=-1.23,arg2=-1.1

The error type = _SING

The error value=-1.#IND

After handling error value=1234

1234

Press any key to continue

第一行为-1.23的倒数,第二~六两行是_matherr函数的输出,第七行是主函数的输出。

也许有人会说,main函数并没有调用_matherr函数,为什么会出现这种情况呢?这就是VC++编译器为我们做的事情了。它很有可能在数学函数中设置了跳转来实现异常处理,当数学库中的符点函数探测到一个错误时,就调用此函数。下面是有关_matherr函数的一些说明:

1、返回值:类型是整型的。按惯例,0返回值用来标志一个错误,非0值标志成功。如果返回0,则错误信息可被显示,错误序号被正确设置。如果返回非0值,没有显示错误信息,错误序号也保持不变。

2、参数:except指针指向一个包含错误信息的结构 struct _exception。

_exception结构包含有如下数据成员:

int type 异常类型;

char *name 出错函数名;

double arg1, arg2 函数的第一和第二(如果有的话)参数;

double retval 函数的返回值。

注意:数学函数的错误类型定义如下:

_DOMAIN 变元定义域错误;

_SING 变元奇异点错误;

_OVERFLOW 溢出错误;

_PLOSS 精度部分遗失;

_TLOSS 精度丢失;

_UNDERFLOW 下溢错误,结果太小,无发表示。

下面是MSDN给我们提供的一个示例供大家参考:

/* MATHERR.C illustrates writing an error routine for math

* functions. The error function must be:

* _matherr

*/

#include <math.h

#include <string.h

#include <stdio.h

void main()

{

/* Do several math operations that cause errors. The _matherr

* routine handles _DOMAIN errors, but lets the system handle

* other errors normally.

*/

printf( "log( -2.0 ) = %e\n", log( -2.0 ) );

printf( "log10( -5.0 ) = %e\n", log10( -5.0 ) );

printf( "log( 0.0 ) = %e\n", log( 0.0 ) );

}

/* Handle several math errors caused by passing a negative argument

* to log or log10 (_DOMAIN errors). When this happens, _matherr

* returns the natural or base-10 logarithm of the absolute value

* of the argument and suppresses the usual error message.

*/

int _matherr( struct _exception *except )

{

/* Handle _DOMAIN errors for log or log10. */

if( except-type == _DOMAIN )

{

if( strcmp( except-name, "log" ) == 0 )

{

except-retval = log( -(except-arg1) );

printf( "Special: using absolute value: %s: _DOMAIN "

"error\n", except-name );

return 1;

}

else if( strcmp( except-name, "log10" ) == 0 )

{

except-retval = log10( -(except-arg1) );

printf( "Special: using absolute value: %s: _DOMAIN "

"error\n", except-name );

return 1;

}

}

else

{

printf( "Normal: " );

return 0; /* Else use the default actions */

}

}

输出结果

Special: using absolute value: log: _DOMAIN error

log( -2.0 ) = 6.931472e-001

Special: using absolute value: log10: _DOMAIN error

log10( -5.0 ) = 6.989700e-001

Normal: log( 0.0 ) = -1.#INF00e+000

作者信息:

姓名:周云才

邮箱:zyc262@163.net

联系地址:江汉石油学院计算机系 邮编 434023

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航