forked from thradams/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cCode206.c
47 lines (39 loc) · 1.34 KB
/
cCode206.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
//en.cppreference.com/w/c/numeric/fenv/feupdateenv.html
#include <stdio.h>
#include <fenv.h>
#include <float.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");
}
double x2 (double x) /* times two */
{
fenv_t curr_excepts;
/* Save and clear current f-p environment. */
feholdexcept(&curr_excepts);
/* Raise inexact and overflow exceptions. */
printf("In x2(): x = %f\n", x=x*2.0);
show_fe_exceptions();
feclearexcept(FE_INEXACT); /* hide inexact exception from caller */
/* Merge caller's exceptions (FE_INVALID) */
/* with remaining x2's exceptions (FE_OVERFLOW). */
feupdateenv(&curr_excepts);
return x;
}
int main(void)
{
feclearexcept(FE_ALL_EXCEPT);
feraiseexcept(FE_INVALID); /* some computation with invalid argument */
show_fe_exceptions();
printf("x2(DBL_MAX) = %f\n", x2(DBL_MAX));
show_fe_exceptions();
return 0;
}