What's the difference between the pair of functions floor()/ceil() and min()/max()?
Asked Answered
B

8

17

I would say all programming languages have functions with these names to choose the lesser or greater of two values:

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

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

I've always been a bit fuzzy on the difference between the former pair and the latter pair. I have a vague impression that min/max are more simplistic and floor/ceiling are more mathematical, but that's not much to go on.

Oddly I can't find this discussed anywhere on StackOverflow or the Internet generally by searching Google. So is there some best practices or rules of thumb to decide which of these functions to use when your programming language offers both?

Bacteroid answered 15/3, 2012 at 17:26 Comment(0)
U
19

This is apples vs. oranges. In most languages/APIs, min/max take two (or more) inputs, and return the smallest/biggest. floor/ceil take one argument, and round it down or up to the nearest integer.

Unprejudiced answered 15/3, 2012 at 17:27 Comment(1)
Oof! Seems obvious! But I won't delete it yet in case there are other blockheads out there like me who thought the same thing! \-:Bacteroid
A
15

To my knowledge max and min are used on a collection, say an array of numbers. Floor and ceiling are used for single numbers. For example:

min(1, 2, 3, 4) => 1
max(1, 2, 3, 4) => 4
floor(3.5) => 3
ceiling(3.5) => 4
Apologia answered 15/3, 2012 at 17:27 Comment(0)
R
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)

On the definition:

floor is the greatest integer less than n.

ceil is the smallest integer greater than n.

Rhythmist answered 24/7, 2015 at 4:36 Comment(0)
R
9

In Javascript:

  • Floor Round a number downward to its nearest integer.
  • Ceil Round a number upward to its nearest integer.
  • Round Round a 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>
Regenaregency answered 13/9, 2018 at 13:5 Comment(0)
J
6

I realise this post is old, but an important distinction is being overlooked.

The function round() rounds towards or away from zero, while the functions ceil() and floor() round toward positive infinity and negative infinity;

This is important if dealing with both positive and negative numbers. i.e.

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
Johnathanjohnathon answered 4/4, 2018 at 1:19 Comment(0)
E
3

min() and max() return the smaller or larger of 2 values, some might do more than 2 values, as in

min(3, 5); returns 3.

floor() and ceiling() truncate a double into an integer as in

floor(5.3); returns 5.

Encarnacion answered 15/3, 2012 at 17:29 Comment(0)
L
3

Not sure I understand your question, see for example:

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)
Lapidate answered 15/3, 2012 at 17:29 Comment(0)
J
-1

Please consider below for difference between Math.floor(), Math.round() and Math.ceil() in javascript:

1- Math.floor(x) /if x=0.99 convert to = x=0/ (Returns x rounded down to its nearest integer)

2- Math.ceil(x) /if x<0.1 convert to x=1/(Returns x rounded up to its nearest integer)

3-1- Mathh.round(x) / if x<0.5 convert to x=0/
3-2- Mathh.round(x) / if 0.5<=x<1 convert to x=1/
(Returns x rounded to its nearest integer)

Jaffna answered 23/2, 2022 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.