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

hippietrail picture hippietrail · Mar 15, 2012 · Viewed 34.1k times · Source

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?

Answer

Oliver Charlesworth picture Oliver Charlesworth · Mar 15, 2012

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.