fexcept_t类型
描述:
该类型是表示浮点状态标志的类型,是浮点状态标志的总称,包括实现与标志相关联的任何状态。
(注:根据IEEE 754标准,状态标志是一种可以处于两种状态(置位(raised)状态或者清除(lowered)状态)的变量。当处于置位状态时,状态标志可能传递额外的、与系统相关的信息,这些信息用户可能无法访问。发生异常时,可能会附带地置位以下部分状态标志:不精确(inexact)、下溢(underflow)、溢出(overflow)、除零(divideByZero)以及无效(invalid)操作。)
该类型的具体细节由实现决定,该类型值通过fegetexceptflag函数获取,可以用作fesetexceptflag函数的参数。
范例:
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
46
47
48
49
50
|
/*类型fexcept_t范例*/
#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)
{
fexcept_t flagp;
feclearexcept(FE_ALL_EXCEPT);
feraiseexcept(FE_OVERFLOW|FE_INEXACT);
fegetexceptflag(&flagp,FE_ALL_EXCEPT);
feclearexcept(FE_ALL_EXCEPT);
fesetexceptflag(&flagp,FE_ALL_EXCEPT);
showExceptions();
return 0;
}
|
输出:
Exceptions: FE_INEXACT FE_OVERFLOW
相关内容:
主要参考资料:
1、ISO/IEC 9899:2024
2、ISO/IEC 9899:2018
3、IEEE 754:2019