Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ <cfenv> 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 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_).

Return Value

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

Exception

This function never throws exceptions.

Example:

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

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

using namespace std;

int main (){
  cout<<"Rounding 10.9\n";

  //upward rounding direction mode
  fesetround(FE_UPWARD);
  cout<<"FE_UPWARD: "<<rint(10.9)<<"\n";

  //downward rounding direction mode
  fesetround(FE_DOWNWARD);
  cout<<"FE_DOWNWARD: "<<rint(10.9)<<"\n";

  //toward-zero rounding direction mode
  fesetround(FE_TOWARDZERO);
  cout<<"FE_TOWARDZERO: "<<rint(10.9)<<"\n";

  //to-nearest rounding direction mode
  fesetround(FE_TONEAREST);
  cout<<"FE_TONEAREST: "<<rint(10.9)<<"\n";

  return 0;
}

The output of the above code will be:

Rounding 10.9
FE_UPWARD: 11
FE_DOWNWARD: 10
FE_TOWARDZERO: 10
FE_TONEAREST: 11

❮ C++ <cfenv> Library