forked from thradams/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cCode200.c
71 lines (60 loc) · 1.99 KB
/
cCode200.c
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
//en.cppreference.com/w/c/numeric/fenv/feenv.html
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
printf("current exceptions raised: ");
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");
if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
void show_fe_rounding_method(void)
{
printf("current rounding method: ");
switch (fegetround()) {
case FE_TONEAREST: printf ("FE_TONEAREST"); break;
case FE_DOWNWARD: printf ("FE_DOWNWARD"); break;
case FE_UPWARD: printf ("FE_UPWARD"); break;
case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break;
default: printf ("unknown");
};
printf("\n");
}
void show_fe_environment(void)
{
show_fe_exceptions();
show_fe_rounding_method();
}
int main(void)
{
fenv_t curr_env;
int rtn;
/* Show default environment. */
show_fe_environment();
printf("\n");
/* Perform some computation under default environment. */
printf("+11.5 -> %+4.1f\n", rint(+11.5)); /* midway between two integers */
printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* midway between two integers */
show_fe_environment();
printf("\n");
/* Save current environment. */
rtn = fegetenv(&curr_env);
/* Perform some computation with new rounding method. */
feclearexcept(FE_ALL_EXCEPT);
fesetround(FE_DOWNWARD);
printf("1.0/0.0 = %f\n", 1.0/0.0);
printf("+11.5 -> %+4.1f\n", rint(+11.5));
printf("+12.5 -> %+4.1f\n", rint(+12.5));
show_fe_environment();
printf("\n");
/* Restore previous environment. */
rtn = fesetenv(&curr_env);
show_fe_environment();
return 0;
}