FE_INVALID宏
概要:
#define FE_INVALID value //value值由实现定义,为2的N次方。
描述:
该宏表示域异常,该宏会扩展为int类型的常量表达式,其值是2的N次方。一些数学函数仅对特定的值有效,这些值称为函数的域;当传递给函数的参数超出了函数的域(例如:sqrt(-5.0))时,会引发域异常。
当且仅当实现通过feclearexcept、fegetexceptflag、feraiseexcept、fesetexceptflag、fetestexcept函数支持浮点异常时,宏FE_INVALID才会被定义。
范例:
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_INVALID范例*/
#include <fenv.h>
#include <math.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 result = asin(5);
showExceptions();
return 0;
}
|
输出:
Exceptions: FE_INVALID
在这个例子中,asin函数参数取值范围为[-1,+1],5不在这个范围内,所以发生了域异常。
相关内容: