fegetround, fesetround
From cppreference.com
Defined in header <fenv.h>
|
||
int fesetround( int round ); |
(1) | (since C99) |
int fegetround(); |
(2) | (since C99) |
1) Attempts to establish the floating-point rounding direction equal to the argument round
, which is expected to be one of the floating-point rounding macros.
2) Returns the value of the floating-point rounding macro that corresponds to the current rounding direction.
Parameters
round | - | rounding direction, one of floating-point rounding macros |
Return value
1) 0 on success, non-zero otherwise.
2) the floating-point rounding macro describing the current rounding direction or a negative value if the direction cannot be determined.
Notes
The current rounding mode, reflecting the effects of the most recent fesetround
, can also be queried with FLT_ROUNDS.
Example
Run this code
#include <stdio.h> #include <math.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON void show_fe_current_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"); } int main(void) { /* Default rounding method */ show_fe_current_rounding_method(); 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 method. */ int curr_method = fegetround(); /* Temporarily change current rounding method. */ fesetround(FE_DOWNWARD); show_fe_current_rounding_method(); printf("+11.5 -> %+4.1f\n", rint(+11.5)); printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* Restore default rounding method. */ fesetround(curr_method); show_fe_current_rounding_method(); return 0; }
Possible output:
current rounding method: FE_TONEAREST +11.5 -> +12.0 +12.5 -> +12.0 current rounding method: FE_DOWNWARD +11.5 -> +11.0 +12.5 -> +12.0 current rounding method: FE_TONEAREST
References
- C11 standard (ISO/IEC 9899:2011):
- 7.6.3.1 The fegetround function (p: 212)
- 7.6.3.2 The fesetround function (p: 212-213)
- C99 standard (ISO/IEC 9899:1999):
- 7.6.3.1 The fegetround function (p: 193)
- 7.6.3.2 The fesetround function (p: 193-194)
See also
(C99)(C99)(C99) |
rounds to an integer using current rounding mode (function) |
(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99) |
rounds to an integer using current rounding mode with exception if the result differs (function) |