Related Articles

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

• Last Updated : 20 Nov, 2017

fesetround()

It sets the specified floating point rounding direction or the “current rounding direction” which is expected to be one of the floating point rounding macros.
It is used with rint(), nearbyint() and other rounding functions in C++.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
```Syntax:
int fesetround( int round );
where round can be FE_TONEAREST,
FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO
Return : The fesetround() function returns
0 on success and the rounding direction is applied to the required number.
```

Errors and Exceptions : The function takes only FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO as arguments, otherwise it returns an error.

Application : fesetround() function can be used with rint(), nearbyint() and other rounding functions of the math header to apply the “current rounding direction”

 `// C program to illustrate``// fesetround() function with rint() function`` ` `#include ``#include ``#include ``using` `namespace` `std;``// Driver Program``int` `main()``{``    ``double` `x = 3.7, result;``    ``// setting rounding direction to the nearest integer``    ``fesetround(FE_TONEAREST);``    ``result = rint(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction towards zero``    ``fesetround(FE_TOWARDZERO);``    ``result = rint(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction to DOWNWARD``    ``fesetround(FE_DOWNWARD);``    ``result = rint(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction to UPWARD``    ``fesetround(FE_UPWARD);``    ``result = rint(x);``    ``cout << result << endl;`` ` `    ``return` `0;``}`

Output:

```4
3
3
4```

fegetround()

It is used to obtain the value of the floating point rounding macro that corresponds to the current rounding direction. It is used with rint(), nearbyint() and other rounding functions in C++.

```Syntax :
int fegetround();
No parameter
Return : The fegetround() function returns the
floating point rounding macro describing the current
rounding direction.
Rounding Macros:
1.FE_DOWNWARD
2.FE_TONEAREST
3.FE_TOWARDZERO
4.FE_UPWARD
```

Errors and Exceptions :

1. The function does not take any arguments, therefore returns an error if a parameter is passed
2. If no rounding direction is specified using fesetround(), it returns the macro FE_TONEAREST

Application : fegetround() function can be used with rint(), nearbyint() and other rounding functions of the math header to get the “current rounding direction” using macros.

In this program we will be checking and printing the macro returned by the fegetround() function.

 `// C program to illustrate``// fegetround() function with``// rint() function using switch case`` ` `#include ``#include ``#include ``#pragma STDC FENV_ACCESS ON``using` `namespace` `std;`` ` `void` `direction()``{``    ``switch` `(fegetround()) {``    ``case` `FE_TONEAREST:``        ``cout << ``"FE_TONEAREST"``;``        ``break``;``    ``case` `FE_DOWNWARD:``        ``cout << ``"FE_DOWNWARD"``;``        ``break``;``    ``case` `FE_UPWARD:``        ``cout << ``"FE_UPWARD"``;``        ``break``;``    ``case` `FE_TOWARDZERO:``        ``cout << ``"FE_TOWARDZERO"``;``        ``break``;``    ``default``:``        ``cout << ``"unknown"``;``    ``};``    ``cout << endl;``}``// Driver Program``int` `main()``{``    ``double` `x = 3.7;``    ``fesetround(FE_UPWARD);``    ``rint(x);``    ``direction();`` ` `    ``fesetround(FE_DOWNWARD);``    ``rint(x);``    ``direction();`` ` `    ``fesetround(FE_TOWARDZERO);``    ``rint(x);``    ``direction();`` ` `    ``fesetround(FE_TONEAREST);``    ``rint(x);``    ``direction();`` ` `    ``return` `0;``}`

Output:

```FE_UPWARD
FE_DOWNWARD
FE_TOWARDZERO
FE_TONEAREST```

My Personal Notes arrow_drop_up