C programming ceil , ceilf and ceill math.h


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

https://corecplusplustutorial.com/?p=2188&preview=true
Parameters:
x -The value computed to integer not less than ‘x’.

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

All the functions are the same,they compute the ceiling of x.The 1st,2nd and the 3rd function have the same name i.e.’ceil’.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) ceil(x) is same as C math.h ceil,ceilf and ceill 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.

C math ceil x-axis

Code example

double d=1.234 , x1=2.9877800e20 ;

float f=1.234 ;

long double ld=1.234 ;

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

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

Output in Visual Studio ,

2.000000
2.000000
2.000000
298778000000000000000.000000
-nan(ind)

For the 5th output,if ‘NaN’ is passed ‘nan’ is to be expected.


 


ceilf

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

Code example

float x=50.01 ;

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

Output in Visual Studio,

51.000000
51.000000


ceill

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

Output,

51.000000
51.000000


*Side Note

The ceil function behave as though it is defined as:

  #include <cmath>
  #include <cfenv>

  #pragma STDC FENV_ACCESS ON

 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;
  }