What is the difference between the pairs of functions floor () / ceil () and min () / max ()?

I would say that all programming languages ​​have functions with these names to select the smaller or the larger of the two values:

  • min() and max()
  • floor()and ceil()/ceiling()

And some languages ​​have both. JavaScript, I believe, is one example.

I was always a little blurred in the difference between the previous pair and the last pair. I have a vague impression that min/ maxare more simplified and floor/ are ceilingmore mathematical, but this is not so much.

Oddly enough, I can't find this anywhere on StackOverflow or the web, searching Google. So, are there some guidelines or rules of thumb to decide which of these features to use when your programming language offers both?

+6
source share
8 answers

These are apples against oranges. In most languages, / APIs, min/ maxtake two (or more) inputs and return the smallest / largest. floor/ ceiltakes one argument and rounds it to the nearest integer.

+10
source

, max min , . Floor ceiling . :

min(1, 2, 3, 4) => 1
max(1, 2, 3, 4) => 4
floor(3.5) => 3
ceiling(3.5) => 4
+12
min(1, 2) == 1
max(1, 2) == 2

floor(3.9) == 3
round(3.9) == 4

ceil(3.1) == 4
round(3.1) == 3

trunc, as in (int)(3.xxx) = 3 (no matter what xxx is)

:

floor - n.

ceil - , n.

+7

min() max() 2 , 2 ,

min(3, 5); 3.

floor() ceiling() ,

floor(5.3); 5.

+3

, , :

a = 1.7
b = 2.8

min(a,b) = 1.7 (returns the minimum of a and b)
max(a,b) = 2.8 (returns the maximum of a and b)

floor(a) = 1 (rounds towards 0)
floor(b) = 2 (rounds towards 0)

ceil(a) = 2 (rounds up)
ceil(b) = 3 (rounds up)
+3

, , .

round() , ceil() floor() ;

, . ..

round(4.900000) = 5.000000
ceil(4.900000) = 5.000000
floor(4.900000) = 4.000000

round(4.100000) = 4.000000
ceil(4.100000) = 5.000000
floor(4.100000) = 4.000000

round(-4.100000) = -4.000000
ceil(-4.100000) = -4.000000
floor(-4.100000) = -5.000000

round(-4.900000) = -5.000000
ceil(-4.900000) = -4.000000
floor(-4.900000) = -5.000000
+3

Javascript:

  • .
  • Ceil Round .
  • Round Round - the number to the nearest integer.

function ceil() {
    var d = Math.ceil(5.1);

    var x = d + "<br>";
    document.getElementById("ceil").innerHTML = x; 
}

function floor() {
    var d = Math.floor(5.1);

    var x = d + "<br>";
    document.getElementById("floor").innerHTML = x; 
}

function round() {
    var d = Math.round(5.1);

    var x = d + "<br>";
    document.getElementById("round").innerHTML = x; 
}
<!DOCTYPE html>
<html>
<body>

<p>Click the different buttons to understand the concept of Ceil, Floor and Round</p>

<button onclick="ceil()">Ceil 5.1</button>
<p id="ceil"></p>

<button onclick="floor()">Floor 5.1</button>
<p id="floor"></p>

<button onclick="round()">Round 5.1</button>
<p id="round"></p>
</body>
</html>
Run codeHide result
+2
source

All the answers above are correct ^, I just want to add the answer that if I use python ... ceil and floor is part of the Math module, so I need to import it if I want to use it. But the circle, min, max, it is not necessary. Example:

import math
print(math.floor(2.9)) 

sorry OOT;)

0
source

All Articles