C Standard Library

C <fenv.h> - fesetround() Function



The C <fenv.h> fesetround() function attempts to set the current floating-point rounding direction mode as specified by rdir. Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Syntax

int fesetround (int rdir);

Parameters

rdir    

Specify rounding direction, one of floating-point rounding macros. All possible rounding direction modes are:

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

Return Value

Returns 0 if the requested rounding direction is successfully set, non-zero otherwise.

Exception

This function never throws exceptions.

Example:

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

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

#pragma STDC FENV_ACCESS ON

int main (){
  printf("Rounding 10.9\n");

  //upward rounding direction mode
  fesetround(FE_UPWARD);
  printf("FE_UPWARD: %.1f\n", rint(10.9));

  //downward rounding direction mode
  fesetround(FE_DOWNWARD);
  printf("FE_DOWNWARD: %.1f\n", rint(10.9));

  //toward-zero rounding direction mode
  fesetround(FE_TOWARDZERO);
  printf("FE_TOWARDZERO: %.1f\n", rint(10.9));

  //to-nearest rounding direction mode
  fesetround(FE_TONEAREST);
  printf("FE_TONEAREST: %.1f\n", rint(10.9));

  return 0;
}

The output of the above code will be:

Rounding 10.9
FE_UPWARD: 11.0
FE_DOWNWARD: 10.0
FE_TOWARDZERO: 10.0
FE_TONEAREST: 11.0

❮ C <fenv.h> Library