# C++ cmath floor , floorf and floorl function

The C++ <cmath> floor,floorf and floorl function compute the largest integer value not greater than the argument.The declaration of the functions are given below.

1 float floor(float x);
2 double floor(double x);
3 long double floor(long double x);
4 float floorf(float x);
5 long double floorl(long double x);

Parameters:
x -The argument whose largest integer is to be computed.

Return type
floating point -The largest integer value not greater than x.

Some points to note:

i) floor(x) is same as .

ii) If ±0 is passed ±0 is returned.

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

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

Using floor function to convert the floating point type to int type is safer than using the casting method,due to it safer way of handling the floating point value without causing any overflow.Note in x-axis the floor function round the argument to the nearest integer in the left direction.

Code example

/*Passing floating point value*/
cout<< “floor(6.7)=” << floor( 6.7 ) << endl;

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

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

Output

floor( 6.7 )= 6
floor( NAN )= nan
floor( -INFINITY )= -inf

#### floorf and floorl

*Note

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

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

Code example

#include <typeinfo>

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

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

Output

*floorf
f
f
*floorl
e (means ‘long double’)
e (means ‘long double’)