Open In App

Difference Between Floor and Ceil Function

Improve
Improve
Like Article
Like
Save
Share
Report

Rational number, in arithmetic, is a number that can be represented as the quotient p/q of two integers such that q ≠ 0. In addition to all the fractions, the set of rational numbers includes all the integers, each of which can be written as a quotient with the integer as the numerator and 1 as the denominator.

So, basically rational number is used to represent values that are present in between the integers.

For eg. 3.850 is present between 3 and 4.

In a few cases, we have to take integers values as answers whereas we are getting rational numbers as answers.

So, in such cases, we use floor and ceiling functions.

Ceil Function

Ceil means ceiling of our home. So, we can pick out some similarities from here. That is ceil function returns an integer that is just greater than a certain rational value.

Ceil function is used in situations where exact integer values are required which is just greater than or equal to the given value.

For example, ceil value of 3.138 is 4.

It should be noted that this does not return the round-off value which is 3. It just returns an integer that is just greater than a certain rational value.

Example 1: Find ceil value of 5.534.

Solution: Ceil value of 5.534 is an just greater than 5.534 i.e. 6.

So, the ceil value of 5.534 is 6.

Example 2: What is ceil(0.34).

Solution: Ceil value of 0.34 is integer just greater than 0.34 i.e. 1.

So, ceil(0.34) is 1.

Floor function

Floor means the floor of our home. So, we can pick out some similarities from here. That is floor function returns an integer that is just lesser than a certain rational value.

Floor function is used in situations where exact integer values are required which is just lesser than or equal to the given value.

For example, ceil value of 3.883 is 3.

It should be noted that this does not return the round-off value which is 4. It just returns an integer that is just lesser than a certain rational value.

Example 1: What is value of floor(86.43)?

Solution: floor value of 86.43 is integer value just lesser than 86.43 that is 86.

So, the value of floor(86.43) is 86.

Example 2: What is floor of 67.212?

Solution: floor of 67.212 is integer just lesser than 67.212 that is 67.

So, floor of 67.212 is 67.

Following is a table of Differences between Floor and Ceil Function:

 

Floor Function

Ceil Function

1. ‘floor’ means the floor of our home. ‘ceil’ means roof or ceiling of our home.
2. floor function returns the integer value just lesser than the given rational value. ceil function returns the integer value just greater than the given rational value.
3. It is represented as floor(x). It is represented as ceil(x)
4. The floor of negative fractional numbers is represented using the floor function. The ceil of negative fractional numbers are represented using ceil function.
5. It returns the value which is just less than or equal to the given value. It returns the value which is just greater than or equal to the given value.
6. It is present in the math header file in C++ and in the Math class in java. It is present in the math header file in C++ and in the Math class in java.
7. Eg. the floor of 78.38 is 78 and the floor of -39.78 is -40. Eg. ceil of 78.38 is 79 and ceil of -39.78 is -39.

The above explanation is just a mathematical explanation of ceil and floor function. Now just see the basic programming implementation of this:

  • Ceil and floor functions are in-built functions present in most programming languages.
  • They take the floating value as an input parameter and returns an integer as an output parameter.
  • No need to worry if you don’t know how to program. It is just a simple input-output program.

C++




// C++ program to illustrate ceil and floor functions
#include <bits/stdc++.h>
using namespace std;
int main()
{
    cout <<"ceil of "<<5.384<<" is "<< ceil(5.384) << endl;
    cout <<"floor of "<<3.184<<" is "<< floor(3.184) << endl;
    cout <<"ceil of "<<6.892<<" is "<< ceil(6.892) << endl;
    cout <<"floor of "<<15.764<<" is "<< floor(15.764) << endl;
    return 0;
}
  
// This code is contributed by Devansh Thakur.


Java




// Java program to illustrate ceil and floor functions.
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        System.out.println("ceil of " + 5.384 + " is "
                           + (int)Math.ceil(5.384));
        System.out.println("ceil of " + 3.184 + " is "
                           + (int)Math.floor(3.184));
        System.out.println("ceil of " + 6.892 + " is "
                           + (int)Math.ceil(6.892));
        System.out.println("ceil of " + 15.764 + " is "
                           + (int)Math.floor(15.764));
    }
}
  
// This code is contributed by Devansh Thakur.


Python3




# Python program to illustrate ceil and floor functions.
import math
  
# code
print("ceil of ", 5.384, " is ", math.ceil(5.384))
print("ceil of ", 3.184, " is ", math.floor(3.184))
print("ceil of ", 6.892, " is ", math.ceil(6.892))
print("ceil of ", 15.764, " is ", math.floor(15.764))
  
# This code is contributed by Devansh Thakur.


Output

ceil of 5.384 is 6
floor of 3.184 is 3
ceil of 6.892 is 7
floor of 15.764 is 15


Last Updated : 05 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads