C programming rint, rintf and rintl math.h


In C programming the <math.h> 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.

All the functions are the same,they compute the rounded integer value of the floating point argument.The 1st,2nd and the 3rd function have the same name i.e.’rint’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

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.

Link :C programming nearbyint,nearbytinf and nerabyintl

Code example

double d=5.534 , x1=298778.9e30 ;

float f=5.534 ;

long double ld=5.534 ;

printf(“%lf”, nearbyint( d ) ) ; //calls the 2nd version
printf(“\n%f”, nearbyint( f ) ) ; //calls the 1st version
printf(“\n%Lf”, nearbyint( ld ) ) ;//calls the 3rd version

printf(“\n%lf”, nearbyint( x1 ) ) ;
printf(“\n%lf”, nearbyint( NAN ) ) ;

Output in Visual Studio ,

6.000000
6.000000
6.000000
298778899999999999319435656585805824.000000
-nan(ind)

In the 4th output the argument passed is 298778.9e30 but the output doesn’t seem to be accurate,this is because a floating point type can represent a value accurately only up to 6 precise value.

Link :C and C++ precision and accuracy





rintf

The ‘rintf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘rintf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=59.901 ;

printf(“%f”, rint( x ) ); //Calls the 1st version
printf(“\n%f”, rintf( 59.901) ); Calls the 4th version

Output in Visual Studio,

60.000000
60.000000


rintl

The ‘rintl‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘rintl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=59.901;

printf(“%Lf”, nearbyint( x ) ); //Calls the 3rd version
printf(“\n%Lf”, nearbyintl( 59.901 ) ); Calls the 5th version

Output,

60.000000
60.000000


Related links

->C floor,floorf and floorl

->C ceil,ceilf and ceill