fegetenv函数
概要:
#include <fenv.h>
int fegetenv(fenv_t *envp);
描述:
该函数尝试将当前浮点环境存储到参数envp指向的对象中。
函数fegetenv、feholdexcept、fesetenv、feupdateenv将浮点环境(状态标志和控制模式)作为一个整体操作。
参数:
fenv_t *envp
参数为一个指向fenv_t类型对象的指针,当前浮点环境将存储到其指向的对象中。
返回值:
如果成功存储当前浮点环境,函数返回0;否则函数返回一个非0值。
范例:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*函数fegetenv范例*/
#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("");
}
}
void showRoundingDirection(void)
{
printf("Rounding direction: ");
switch(fegetround())
{
case FE_DOWNWARD:
puts("FE_DOWNWARD");
break;
case FE_TOWARDZERO:
puts("FE_TOWARDZERO");
break;
case FE_UPWARD:
puts("FE_UPWARD");
break;
default:
puts("FE_TONEAREST");
break;
}
}
void showFloatingPointEnvironment(void)
{
showExceptions();
showRoundingDirection();
puts("");
}
int main(void)
{
fenv_t defaultEnv;
/*获取默认浮点环境。*/
fegetenv(&defaultEnv);
puts("Default floating-point environment:");
showFloatingPointEnvironment();
/*当前浮点环境。*/
feclearexcept(FE_ALL_EXCEPT);
feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW);
fesetround(FE_DOWNWARD);
puts("Current floating-point environment:");
showFloatingPointEnvironment();
/*将浮点环境设置为默认浮点环境。*/
puts("Set floating-point environment to default floating-point environment:");
fesetenv(&defaultEnv);
showFloatingPointEnvironment();
return 0;
}
|
输出:
Default floating-point environment:
Exceptions: No floating-point status flag is set.
Rounding direction: FE_TONEAREST
Current floating-point environment:
Exceptions: FE_DIVBYZERO FE_OVERFLOW
Rounding direction: FE_DOWNWARD
Set floating-point environment to default floating-point environment:
Exceptions: No floating-point status flag is set.
Rounding direction: FE_TONEAREST
注:使用GCC编译。
相关内容: