📜  Floor 和 Ceil函数之间的区别

📅  最后修改于: 2022-05-13 01:54:12.955000             🧑  作者: Mango

Floor 和 Ceil函数之间的区别

有理数,在算术中,是可以表示为两个整数的商 p/q 使得 q ≠ 0 的数。除了所有分数之外,有理数集还包括所有整数,每个整数可以是写成商,整数为分子,1为分母。

因此,基本上有理数用于表示整数之间存在的值。

例如。 3.850 位于 3 和 4 之间。

在少数情况下,我们必须将整数值作为答案,而我们将有理数作为答案。

因此,在这种情况下,我们使用地板天花板功能。

细胞函数

Ceil 意思是我们家的天花板。所以,我们可以从这里挑出一些相似之处。也就是说 ceil函数返回一个刚好大于某个有理值的整数。

Ceil函数用于需要精确整数值且刚好大于或等于给定值的情况。

例如,3.138 的 ceil 值为 4。

需要注意的是,这不会返回舍入值 3。它只是返回一个刚好大于某个有理值的整数。

楼层函数

地板是指我们家的地板。所以,我们可以从这里挑出一些相似之处。即 floor函数返回一个整数,该整数刚好小于某个有理值。

地板函数用于需要精确整数值且恰好小于或等于给定值的情况。

例如,3.883 的 ceil 值为 3。

应该注意的是,这不会返回四舍五入的值,它只是返回一个小于某个有理值的整数。

下面是 Floor 和 Ceil函数之间的差异表:

 

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.

上面的解释只是 ceil 和 floor函数的数学解释。现在看看这个的基本编程实现:

  • Ceil 和 floor 函数是大多数编程语言中存在的内置函数。
  • 它们将浮点值作为输入参数,并返回一个整数作为输出参数。
  • 如果您不知道如何编程,无需担心。它只是一个简单的输入输出程序。
C++
// C++ program to illustrate ceil and floor functions
#include 
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.


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