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

FE_INEXACT宏


概要:
#include <fenv.h>
#define FE_INEXACT value	//value值由实现定义,为2的N次方。

描述:

该宏表示不精确结果异常,该宏会扩展为int类型的常量表达式,其值是2的N次方。当函数由于某种原因不能得到精确的结果或者操作的返回类型不能以指定的精度表示结果时,会引发不精确结果异常。

当且仅当实现通过feclearexcept、fegetexceptflag、feraiseexcept、fesetexcept、fesetexceptflag、fetestexceptflag、fetestexcept函数支持浮点异常时,宏FE_INEXACT才会被定义。

十进制浮点运算与ISO/IEC 60559标准中二进制浮点运算访问相同的浮点异常状态标志。


范例:
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 
/*宏FE_INEXACT范例*/

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

#pragma STDC FENV_ACCESS ON

void showExceptions(void)
{
    printf("Exceptions: ");
    /*判断是否设置浮点异常。*/
    if(fetestexcept(FE_ALL_EXCEPT)==0)
    {
        puts("No floating-point status flag is set.");
    }
    else
    {
    	/*判断设置的浮点异常。*/
        if(fetestexcept(FE_DIVBYZERO))
            printf("FE_DIVBYZERO ");
        if(fetestexcept(FE_INEXACT))
            printf("FE_INEXACT ");
        if(fetestexcept(FE_INVALID))
            printf("FE_INVALID ");
        if(fetestexcept(FE_OVERFLOW))
            printf("FE_OVERFLOW ");
        if(fetestexcept(FE_UNDERFLOW))
            printf("FE_UNDERFLOW ");
        puts("");
    }
    
    /*清除设置的浮点异常。*/
    feclearexcept(FE_ALL_EXCEPT);
}

int main(void)
{
    feclearexcept(FE_ALL_EXCEPT);
    double number = 1.0;
    double result = number/3.0;
    showExceptions();

    return 0;
}


输出:

Exceptions: FE_INEXACT


相关内容:
FE_ALL_EXCEPT 表示所有异常的宏。
FE_DIVBYZERO 表示除以0异常的宏。
FE_INVALID 表示无效操作异常的宏。
FE_OVERFLOW 表示上溢异常的宏。
FE_UNDERFLOW 表示下溢异常的宏。


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018

3、IEEE 754:2019