# C programming nearbyint , nearbyintf and nearbyintl math.h

In C programming the <math.h> nearbyint,nearbyintf and nearbyintl round the argument using the current rounding direction.The declaration of the functions are given below.

1 float nearbyint(float x);
2 double nearbyint(double x);
3 long double nearbyint(long double x);
4 float nearbyintf(float x);
5 long double nearbyintl(long double x);

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

Return type
floating point -The nearby integer of the floating point value.

All the functions are the same,they round the argument using the current rounding direction.The 1st,2nd and the 3rd function have the same name i.e.’nearbyint’.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 rounding is done using the current rounding direction.Simply put if the fractional value is less than 5,the fractional part is dropped and the integer is taken.If the fractional value is equal to or more than 5 than the integer value just next to the value is taken.

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

iii) If ±∞ is passed ±∞ is returned.

Code example

double d=1.234 , x1=2.9877800e20 ;

float f=1.234 ;

long double ld=1.234 ;

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 ,

1.000000
1.000000
1.000000
298779.000000
-nan(ind)

#### nearbyintf

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

Code example

float x=50.01 ;

printf(“%f”, nearbyint( x ) ); //Calls the 1st version
printf(“\n%f”, nearbyintf( 50.01) ); Calls the 4th version

Output in Visual Studio,

50.000000
50.000000

#### nearbyintl

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

Code example

long double x=50.01;

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

Output,

50.000000
50.000000

*Side Note

nearbyint does not raise the ‘inexact’ floating-point exception if the result differs in value from the argument.This is also the difference between rint function which raise the ‘inexact’ floating-point exception if the result differs in value from the argument.