当前位置: C语言 -- 标准库 -- <fenv.h> -- fetestexcept

fetestexcept函数


概要:
#include <fenv.h>
int fetestexcept(int excepts);

描述:

该函数确定当前设置的浮点异常标志的指定子集;参数excepts指定要查询的浮点状态标志。这种机制允许只用一次函数调用来测试多个浮点异常。

函数fetestexceptferaiseexceptfeclearexcept支持设置或者清除浮点状态标志的基本抽象。具体实现中,浮点状态标志可能包含更多信息,例如:首次引发浮点异常的代码地址;函数fegetexceptflagfesetexceptflag能够处理浮点状态标志的全部内容。


参数:
int excepts

参数为位掩码值,表示浮点异常的子集,可以是0,也可以是一个或者多个实现支持的浮点异常宏的按位或运算值(例如:FE_OVERFLOW|FE_INEXACT)。如果参数是其它值,函数行为是未定义的。


返回值:

函数返回参数excepts中包含的当前设置的浮点异常对应的浮点异常宏的按位或运算值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
/*函数fetestexcept范例*/

#include <fenv.h>
#include <stdio.h>

void showExceptions(double x)
{
    #pragma STDC FENV_ACCESS ON

    feclearexcept(FE_ALL_EXCEPT);

    /*生成异常。*/
    if(x==0.0)
        feraiseexcept(FE_DIVBYZERO);
    if(x>=1000.0)
        feraiseexcept(FE_INEXACT|FE_OVERFLOW);
    if(x<=-1000.0)
        feraiseexcept(FE_INEXACT|FE_UNDERFLOW);

    /*判断设置的异常种类。*/
    int setExceptions;
    setExceptions = fetestexcept(FE_ALL_EXCEPT);

    puts("The following exceptions are set:");
    if(setExceptions==0)
        puts("No floating-point status flag is set.");
    if(setExceptions&FE_DIVBYZERO)
        puts("FE_DIVBYZERO");
    if(setExceptions&FE_INEXACT)
        puts("FE_INEXACT");
    if(setExceptions&FE_OVERFLOW)
        puts("FE_OVERFLOW");
    if(setExceptions&FE_UNDERFLOW)
        puts("FE_UNDERFLOW");
}

int main(void)
{
    showExceptions(10.0);
    showExceptions(1500.0);
    showExceptions(-1500.0);

    return 0;
}


输出:

The following exceptions are set:

No floating-point status flag is set.

The following exceptions are set:

FE_INEXACT

FE_OVERFLOW

The following exceptions are set:

FE_INEXACT

FE_UNDERFLOW


相关内容:
feclearexcept 清除浮点异常的函数。
fegetexceptflag 获取浮点异常标志的函数。
feraiseexcept 引发浮点异常的函数。
fesetexceptflag 设置浮点异常标志的函数。