function
<cfenv>

fegetround

int fegetround (void);
Get rounding direction mode
Returns a value that indicates the rounding direction mode, as specified in the current floating point environment.

Whether the value returned by this function is the same as FLT_ROUNDS in <cfloat> is unspecified.

Parameters

none

Return Value

If the current rounding mode was successfully determined by the function and is supported by the implementation, the function returns a value for which a corresponding macro is defined:
macro valuedescription
FE_DOWNWARDRound downward.
FE_TONEARESTRound to nearest.
FE_TOWARDZERORound toward zero.
FE_UPWARDRound upward.
Certain library implementations may support additional floating-point rounding directions values (with their corresponding macros also beginning with FE_).
Libraries may define in <fenv.h> only the macro values above they support (the others may not be defined).
At least all of the above macro values are defined in <cfenv>.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* fegetround / rint example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* fegetround, FE_* */
#include <math.h>       /* rint */

int main ()
{
  printf ("rounding using ");
  switch (fegetround()) {
    case FE_DOWNWARD: printf ("downward"); break;
    case FE_TONEAREST: printf ("to-nearest"); break;
    case FE_TOWARDZERO: printf ("toward-zero"); break;
    case FE_UPWARD: printf ("upward"); break;
    default: printf ("unknown");
  }
  printf (" rounding:\n");

  printf ( "rint (2.3) = %.1f\n", rint(2.3) );
  printf ( "rint (3.8) = %.1f\n", rint(3.8) );
  printf ( "rint (-2.3) = %.1f\n", rint(-2.3) );
  printf ( "rint (-3.8) = %.1f\n", rint(-3.8) );
  return 0;
}

Possible output:

Rounding using to-nearest rounding:
rint (2.3) = 2.0
rint (3.8) = 4.0
rint (-2.3) = -2.0
rint (-3.8) = -4.0


Data races

Each thread maintains a separate floating-point environment with its own state. Spawning a new thread copies the current state. [This applies to C11 and C++11 implementations]

Exceptions

No-throw guarantee: this function never throws exceptions.

See also