forked from thradams/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cCode204.c
41 lines (35 loc) · 1.27 KB
/
cCode204.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
//en.cppreference.com/w/c/numeric/fenv/feround.html
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
void show_fe_current_rounding_direction(void)
{
printf("current rounding direction: ");
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");
}
int main(void)
{
/* Default rounding direction */
show_fe_current_rounding_direction();
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 */
/* Save current rounding direction. */
int curr_direction = fegetround();
/* Temporarily change current rounding direction. */
fesetround(FE_DOWNWARD);
show_fe_current_rounding_direction();
printf("+11.5 -> %+4.1f\n", rint(+11.5));
printf("+12.5 -> %+4.1f\n", rint(+12.5));
/* Restore default rounding direction. */
fesetround(curr_direction);
show_fe_current_rounding_direction();
return 0;
}