Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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
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.

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>

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

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.

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top