# C programming round , roundf and roundl math.h

In C programming the <math.h> round,roundf and roundl function return the rounded integer value in floating-point format.The declaration of the functions are given below.

1 float round(float x);
2 double round(double x);
3 long double round(long double x);
4 double roundf(float x);
5 long double roundl(long double x);

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

Return type
floating point -A rounded integer value.

All the functions are the same,they return the rounded integer value in floating-point format.The 1st,2nd and the 3rd function have the same name i.e.’round’.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) A round function round the argument halfway cases away from zero regardless of the current rounding direction.

ii) Passing ±0 returns ±0.

iii) Passing NAN returns ‘nan’.

iv) Passing ±∞ returns ±∞.

Code example

double d=10.34 , x1=298778.9e30 ;

float f=10.34 ;

long double ld=10.34 ;

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

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

Output in Visual Studio ,

10.000000
10.000000
10.000000
298778899999999999319435656585805824.000000
-nan(ind)

#### roundf

The ‘roundf‘ 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 ‘roundf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=59.901 ;

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

Output in Visual Studio,

60.000000
60.000000

#### roundl

The ‘roundl‘ 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 ‘roundl‘ 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”, round( x ) ); //Calls the 3rd version
printf(“\n%Lf”, roundl( 59.901 ) ); Calls the 5th version

Output,

60.000000
60.000000

*Side Note

The double function may be written as

#include <math.h>
#include <fenv.h>

#pragma STDC FENV_ACCESS ON

double round( double x )
{
double result;
fenv_t save_env;
feholdexcept( &save_env );
result = rint( x );
if ( fetestexcept(FE_INEXACT) )
{
fesetround( FE_TOWARDZERO );
result = rint( copysign( 0.5 + fabs( x ) , x ));
}

feupdateenv( &save_env);
return result ;

}