# fesetround() and fegetround() in C++

Hey, guys today we are going to learn about fesetround() and fegetround() functions in C++. These functions are part of the <cfenv> header file which was originally known as <fenv.h> header file in standard C library. fenv here means floating-point environment. These functions are always used with rounding off functions like rint() and nearbyint(). Before we discuss fesetround() and fegetround() functions in detail, let’s briefly learn about rint() and nearbyint() functions .

### rint() and nearbyint() functions in C++

These functions are part of <math.h> or <cmath> header files(Depending on compiler and version used)
rint(): It rounds the floating-point number passed as argument to an integer using the current rounding mode.
nearbyint(): It is similar to rint() except that it does not raise FE_INEXACT exceptions as rint().

## fesetround() function in C++

This function sets the current floating-point rounding direction to one of the floating-point rounding macros. The floating-point rounding macros which can be passed  to fesetround() are:

• FE_TONEAREST : It rounds a floating-point number to the nearest integer.
• FE_UPWARD: It rounds a floating-point number to the integer greater than it(Upward direction)
• FE_DOWNWARD: It rounds a floating-point number to the smaller  integer to it(Downward  direction)
• FE_TOWARDZERO: It rounds a floating-point number towards the direction of zero.

Before we move to fegetround() let’s understand an example of fesetround() function

```#include <iostream>
#include<cfenv>
#include<math.h>
using namespace std;

int main()
{
//lets take 5 random variables a,b,c,d,e of float datatype
float a,b,c,d,e;
a=15.9213;
fesetround(FE_TONEAREST);
b=rint(a);
fesetround(FE_UPWARD);
c=rint(a);
fesetround(FE_DOWNWARD);
d=rint(a);
fesetround(FE_TOWARDZERO);
e=rint(a);
cout<<b<<"\n"<<c<<"\n"<<d<<"\n"<<e;

return 0;
}```

```16
16
15
15

```

## fegetround() function in C++

This function returns the value of floating-point rounding macro corresponding to the current floating-point rounding macro. Thus this function is used to obtain the value of current rounding mode. This function does not require any argument. Let’s understand more with an example:

```#include <iostream>
#include<cfenv>
#include<math.h>

using namespace std;
//we created a function current_round_mode by using fegetround() and switch case to obtain current floating point rounding macro
void current_round_mode()
{
switch(fegetround())
{
case FE_TONEAREST: cout<<"\n Current rounding mode is FE_TONEAREST ";
break;
case FE_UPWARD: cout<<"\n Current rounding mode is FE_UPWARD ";
break;
case FE_DOWNWARD: cout<<"\n Current rounding mode is FE_DOWNWARD ";
break;
case FE_TOWARDZERO: cout<<"\n Current rounding mode is FE_TOWARDZERO ";
break;
}
}

int main()
{
//lets take 5 random variables a,b,c,d,e of float datatype
float a,b,c,d,e;
a=15.9213;
//we set current floating point rounding macro to different values and checked the results
fesetround(FE_TONEAREST);//current floating point rounding macro set to FE_TONEAREST using fesetround()
b=rint(a);
current_round_mode();
cout<<"\n"<<b;
fesetround(FE_UPWARD);//current floating point rounding macro set to FE_UPWARD using fesetround()
c=rint(a);
current_round_mode();
cout<<"\n"<<c;
fesetround(FE_DOWNWARD);//current floating point rounding macro set to FE_DOWNWARD using fesetround()
d=rint(a);
current_round_mode();
cout<<"\n"<<d;
fesetround(FE_TOWARDZERO);//current floating point rounding macro set to FE_TOWARDZERO using fesetround()
e=rint(a);
current_round_mode();
cout<<"\n"<<e;

return 0;
}```

#### Output:

```Current rounding mode is FE_TONEAREST
16
Current rounding mode is FE_UPWARD
16
Current rounding mode is FE_DOWNWARD
15
Current rounding mode is FE_TOWARDZERO
15```

Also, refer :