C++ cmath ceil , ceilf and ceill function

The C++ <cmath> ceil,ceilf and ceill function compute the ceiling of the argument-compute the smallest integer not less than x.The declaration of the functions are given below.

1 float ceil(float x);
2 double ceil(double x);
3 long double ceil(long double x);
4 float ceilf(float x);
5 long double ceill(long double x);

x -The value computed to integer not less than ‘x’.

Return type
floating point -An integer not less then x.

Some points to note:

  i) ceil(x) is same as cmath ceil function.

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

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

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

Using ceil function to get the integer value is much safer than using the casting method to convert the floating point value to int type.It can manipulate the floating point value without any risk of causing an overflow.Also note in an x-axis the ceil function rounds to the nearest integer in the right direction.

cmath ceil function x-axis

Code example

/*Passing floating point value*/
cout<< “ceil( 23.45 )=” << ceil( 23.45 ) << endl ;

/*Passing -ve value*/
cout<< “ceil( -4.95 )=” << ceil( -45.9 ) << endl ;

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

/*Passing INFINITY value*/
cout<< “ceil( INFINITY )=” << ceil( INFINITY ) << endl ;


ceil( 23.45 )= 24
ceil( -45.9 )= -45
ceil( NAN )= nan
ceil( INFINITY )= inf

ceilf and ceill


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

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

Code example

#include <typeinfo>

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

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


e (means ‘long double’)
e (means ‘long double’)

*Side Note

The ceil function behave as though it is defined as:

  #include <cmath>
  #include <cfenv>


 double ceil(double x)
  double result;
  int save_round = fegetround( ) ;
  fesetround( FE_UPWARD ) ;
  result = rint(x); //or nearbyint instead of rint
  fesetround( save_round );
  return result;

Related links

C programming ceil,ceilf and ceill