Rint(), rintf(), rintl() in C++


Here we will see three functions. These functions are Rint(), rintf() and the rintl(). These functions are used to convert floating point values into rounded format.

The rint() Function

This function is used for rounding floating point value to integer. The syntax is like below. If the result is outside of return type, the domain error may occur. When the argument is 0 or infinity, then it will return unmodified

float rint(float argument)
double rint(double argument)
long double rint(long double argument)

Example

#include <cmath>
#include <iostream>
using namespace std;
main() {
   double a, b, x, y;
   x = 53.26;
   y = 53.86;
   a = rint(x);
   b = rint(y);
   cout << "The value of a: " << a << endl;
   cout << "The value of b: " << b;
}

Output

The value of a: 53
The value of b: 54

The rintf() Function

This is same as rint, but the only difference is here all the parameters and return types are float type. In this example we will use the fesetround() method to specify the round type.

float rintf(float argument)

Example

#include <cmath>
#include <fenv.h>
#include <iostream>
using namespace std;
main() {
   double a, b, x, y;
   x = 53.26;
   y = 53.86;
   fesetround(FE_UPWARD);
   a = rintf(x);
   fesetround(FE_DOWNWARD);
   b = rintf(y);
   cout << "The value of a: " << a << endl;
   cout << "The value of b: " << b;
}

Output

The value of a: 54
The value of b: 53

The rintl() Function

This is same as rint, but the only difference is here all the parameters and return types are of long double type. In this example we will use the fesetround() method to specify the round type.

long double rintl(long double argument)

Example

#include <cmath>
#include <fenv.h>
#include <iostream>
using namespace std;
main() {
   double a, b, x, y;
   x = 53547.55555555555;
   y = 53547.55555555523;
   fesetround(FE_UPWARD);
   a = rintl(x);
   fesetround(FE_DOWNWARD);
   b = rintl(y);
   cout << "The value of a: " << a << endl;
   cout << "The value of b: " << b;
}

Output

The value of a: 53548
The value of b: 53547

Updated on: 30-Jul-2019

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements