C++ cmath rint, rintf and rintl function


The C++ <cmath> rint,rintf and rintl function compute the rounded integer value of the floating point argument.The declaration of the functions are given below.

1 float rint(float x);
2 double rint(double x);
3 long double rint(long double x);
4 float rintf(float x);
5 long double rintl(long double x);

Parameters:
x -The floating point value to be rounded off.

Return type
floating point -The rounded integer value of the argument.

Some points to note:

 i) The rint function round the argument using the current rounding direction.

 ii) The difference between ‘rint’ and ‘nearbyint’ function is that ‘rint’ function may raise the inexact floating point exception if the result differs in value from the argument,whereas ‘nearbyint’ function does not raise such exception.

 iii) If NAN is passed ‘nan’ is returned.

 iv) If ±∞ is passed , ±∞ is returned.

Code example

/*Passing floating point value */
cout<< “rint( 23.4 )=” << rint( 23.4 ) << endl ;
cout<< “rint( 45.67 )=” << rint( 45.67 ) << endl ;

/*Passing NAN */
cout<< “rint( NAN )=” << rint( NAN ) << endl ;

/*Passing INFINITY */
cout<< “rint( -INFINITY )=” << rint( -INFINITY ) << endl ;

Output

rint( 23.4 )= 23
rint( 45.67 )= 46
rint( NAN )= nan
rint( -INFINITY )= -inf


 


rintf and rintl

*Note

i)The 4th version rintf is same as the 1st version float rint(float).The last character ‘f‘ in ‘rintf’ stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version rintl is same as the 3rd version long double rint(long double).The last character ‘l‘ in ‘rintl’ stands for ‘long double’ which signify the argument and return type of the function.

Code example

#include <typeinfo>

/*rintf */
cout<< “*rintf\n”;
float f=2;
cout<< typeid( rintf(2) ).name() << endl /*identify type of rintf returned value */
<< typeid( rint(f) ).name() << endl; /*identify type of rint(float) returned value */

/*rintl*/
cout<< “*rintl\n”;
long double ld=2;
cout<< typeid( rintl(2) ).name() << endl /*indentify type of rintl returned value */
<< typeid( rint(ld) ).name() << endl; /*identify type of rint(long double) returned value*/

Output

*rintf
f
f
*rintl
e (means ‘long double’)
e (means ‘long double’)


Related links

->C programming rint,rintf and rintl