C Standard Library

C <fenv.h> - fegetround() Function



The C <fenv.h> fegetround() function returns the value of the floating-point rounding macro, as specified in the current floating point environment.

The current rounding mode which reflects the effects of the most recent fesetround(), can also be queried with FLT_ROUNDS.

Syntax

int fegetround();

Parameters

No parameter is required.

Return Value

Returns the floating-point rounding macro describing the current rounding direction or a negative value if the direction cannot be determined. All possible rounding direction modes are given below:

MacrosDescription
FE_DOWNWARD Rounding towards negative infinity.
FE_TONEAREST Rounding towards nearest representable value.
FE_TOWARDZERO Rounding towards zero.
FE_UPWARD Rounding towards positive infinity.

Certain library implementations may define additional floating-point rounding direction macro constants (with their corresponding macros also beginning with FE_).

Exception

This function never throws exceptions.

Example:

The example below shows the usage of fegetround() function.

#include <stdio.h>
#include <fenv.h>
#include <math.h>

void Rounding_Direction_Message(void) {
  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(" method:\n");
}

int main (){
  Rounding_Direction_Message();

  printf("rint(10.2): %.1f\n", rint(10.2));
  printf("rint(10.8): %.1f\n", rint(10.8));
  printf("rint(-5.2): %.1f\n", rint(-5.2));
  printf("rint(-5.8): %.1f\n", rint(-5.8));

  return 0;
}

The output of the above code will be:

Rounding using to-nearest method:
rint(10.2): 10.0
rint(10.8): 11.0
rint(-5.2): -5.0
rint(-5.8): -6.0 

❮ C <fenv.h> Library