Fesetround() and fegetround() in C++


Here we will see the fesetround() and fegetround() method in C++. These methods can be found in the cfenv library.

The fesetround() method is used to set the specified floating point rounding direction to the current rounding direction. This is used with rint(), nearbyint() and some other rounding functions in C++.

The syntax is like below −

int fesetround(int round);

The round can be among these FE_TONEAREST, FE_DOWNWARD, FE_UPWARD etc. This function returns 0 when rounding direction is successfully applied to the required manner.

Example

#include <cfenv >
#include <cmath>
#include <iostream>
using namespace std;
main() {
   double x = 4.7, ans;
   fesetround(FE_TONEAREST); //round to nearest integer
   ans = rint(x);
   cout << "Nearest Integer is: " << ans << endl;
   fesetround(FE_TOWARDZERO); //rounding towards zero
   ans = rint(x);
   cout << "Rounding towards 0, value is: " << ans << endl;
   fesetround(FE_DOWNWARD); //rounding to downwards
   ans = rint(x);
   cout << "Nearest Integer below the number: " << ans << endl;
   fesetround(FE_UPWARD); //rounding to upwards
   ans = rint(x);
   cout << "Nearest Integer above the number: " << ans << endl;
}

Output

Nearest Integer is: 5
Rounding towards 0, value is: 4
Nearest Integer below the number: 4
Nearest Integer above the number: 5

Now let us see the fegetround() method is used to get the floating point rounding macro that corresponds to the current rounding direction. This function is used with rint(), nearbyint() and some other rounding methods in C++.

The syntax is like below −

int fegetround();

This returns the number corresponding to the floating point rounding macros.

  • FE_DOWNWARD
  • FE_TONEAREST
  • FE_TOWARDZERO
  • FE_UPWARD

Example

#include <cfenv >
#include <cmath>
#include <iostream>
using namespace std;
void float_direction() {
   switch (fegetround()) {
      case FE_TONEAREST:
         cout << "Macro is: FE_TONEAREST";
      break;
      case FE_DOWNWARD:
         cout << "Macro is: FE_DOWNWARD";
      break;
      case FE_UPWARD:
         cout << "Macro is: FE_UPWARD";
      break;
      case FE_TOWARDZERO:
         cout << "Macro is: FE_TOWARDZERO";
      break;
      default:
         cout << "unknown";
   };
   cout << endl;
}
main() {
   double x = 4.7, ans;
   fesetround(FE_TONEAREST); //round to nearest integer
   ans = rint(x);
   cout << "Nearest Integer is: " << ans << endl;
   float_direction();
   fesetround(FE_TOWARDZERO); //rounding towards zero
   ans = rint(x);
   cout << "Rounding towards 0, value is: " << ans << endl;
   float_direction();
   fesetround(FE_DOWNWARD); //rounding to downwards
   ans = rint(x);
   cout << "Nearest Integer below the number: " << ans << endl;
   float_direction();
   fesetround(FE_UPWARD); //rounding to upwards
   ans = rint(x);
   cout << "Nearest Integer above the number: " << ans << endl;
   float_direction();
}

Output

Nearest Integer is: 5
Macro is: FE_TONEAREST
Rounding towards 0, value is: 4
Macro is: FE_TOWARDZERO
Nearest Integer below the number: 4
Macro is: FE_DOWNWARD
Nearest Integer above the number: 5
Macro is: FE_UPWARD

Updated on: 30-Jul-2019

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements