C Standard Library

C <fenv.h> - FE_TONEAREST



The FE_TONEAREST macro expands to a value of type int, which can be used with fesetround and fegetround functions to indicate to-nearest rounding direction. Rounding x to-nearest gives the possible value that is nearest to x. For halfway cases, it is rounded away from zero.

Definition in the <fenv.h> header file is:

#define FE_TONEAREST  /* implementation defined */ 

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_).

Example:

The example below shows the usage of FE_TONEAREST macro.

#include <stdio.h>
#include <fenv.h>
#include <math.h>
 
#pragma STDC FENV_ACCESS ON

int main (){
  //to-nearest rounding direction mode
  fesetround(FE_TONEAREST);

  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:

rint(10.2) = 10.0
rint(10.8) = 11.0
rint(-5.2) = -5.0
rint(-5.8) = -6.0

❮ C <fenv.h> Library