Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

max

min

floor

ceil

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?

like image 389
hippietrail Avatar asked Mar 15 '12 17:03

hippietrail


People also ask

What is the difference between ceil and floor functions?

The ceil function returns the smallest integer value which is greater than or equal to the specified number, whereas the floor function returns the largest integer value which is less than or equal to the specified number.

What is the difference between Math ceil () and Math floor ()?

ceil method returns the smallest integer greater than or equal to the value we pass, Math. floor returns the largest or equal integer that is less than the given value.

What are the difference between ceil and floor functions in SAS?

The FLOOR function rounds down. The CEIL function rounds up. The ROUND function rounds to the nearest integer.

What does Ceil () function do?

The ceil() function computes the smallest integer that is greater than or equal to x.


1 Answers

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.

like image 103
Oliver Charlesworth Avatar answered Sep 21 '22 04:09

Oliver Charlesworth