floor and ceil

 

floor and ceil

floor and ceil functions comes under math.h header file.
floor is used to round down the value of the decimal number and ceil is used to round up the value of the decimal number.

consider the following c program

#include<stdio.h>
#include<math.h>
#include<conio.h>
{
float a;
clrscr();
printf(“Enter any decimal number\n”);
scanf(“%f”,&a);
printf(“Using floor the value is %d and using ceil it is %d\n”,floor(a),ceil(a));
getch();
}

Output:
Enter any decimal number
5656.98
Using floor the value is 5656 and using ceil it is 5657

In otherwords floor will neglect the decimal part while ceil will add 1 to the mantissa part.Hence we can say that floor is similar to casting.That means floor(a) is equivalent to (int)a or (long)a

These 2 functions do not depend upon the value after decimal point that means if the program is run with the value 5656.01 then the output will be same.

Similarly there is floorl and also ceill which returns long type integers

 

 


You may also like

Find out what I am doing by following me on Twitter... Tweet This Post!

Leave a Reply

Comment on the next level:

- Enter your info on the left

- Create an avatar with Gravatar!