| Functions | ||||
(C99) | ||||
(C99) | ||||
(C99) | ||||
(C99)(C99) | ||||
fegetroundfesetround (C99)(C99) | ||||
(C99) | ||||
(C99) | ||||
(C99) | ||||
| Macro constants | ||||
(C99) |
Defined in header <fenv.h> | ||
int fesetround(intround); | (1) | (since C99) |
int fegetround(); | (2) | (since C99) |
1) Attempts to establish the floating-point rounding direction equal to the argumentround, which is expected to be one of thefloating-point rounding macros.
2) Returns the value of thefloating-point rounding macro that corresponds to the current rounding direction.
Contents |
| round | - | rounding direction, one offloating-point rounding macros |
1)0 on success, non-zero otherwise.
2) thefloating-point rounding macro describing the current rounding direction or a negative value if the direction cannot be determined.
The current rounding mode, reflecting the effects of the most recentfesetround, can also be queried withFLT_ROUNDS.
#include <fenv.h>#include <math.h>#include <stdio.h> // #pragma STDC FENV_ACCESS ON void show_fe_current_rounding_direction(void){printf("current rounding direction: ");switch(fegetround()){caseFE_TONEAREST:printf("FE_TONEAREST");break;caseFE_DOWNWARD:printf("FE_DOWNWARD");break;caseFE_UPWARD:printf("FE_UPWARD");break;caseFE_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(); return0;}
Possible output:
current rounding direction: FE_TONEAREST+11.5 -> +12.0+12.5 -> +12.0current rounding direction: FE_DOWNWARD+11.5 -> +11.0+12.5 -> +12.0current rounding direction: FE_TONEAREST
(C99)(C99)(C99) | rounds to an integer using current rounding mode (function)[edit] |
(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99) | rounds to an integer using current rounding mode with exception if the result differs (function)[edit] |
C++ documentation forfegetround,fesetround | |