1
$\begingroup$

I read the Wiki, but I am still somewhat confused. Supposedly floor returns the number lowest, and ceil the number highest. However, I am not sure I understand how this works in freehand form, or how I may implement this myself in a specific thing. Would someone please clarify how ceil and floor works, and how it is used/examples? Thanks!!

$\endgroup$
  • 2
    $\begingroup$ Here's how to calculate it: $\lfloor 1.1\rfloor = 1$ or $\lfloor -1.2\rfloor = -2$. $\endgroup$ – Cameron Williams Nov 26 '13 at 1:00
  • $\begingroup$ What's a Willie? $\endgroup$ – Robert Israel Nov 26 '13 at 1:01
  • $\begingroup$ Wiki ... that is what I meant. Cameron, I do not understand $\endgroup$ – jack Nov 26 '13 at 1:03
  • 1
    $\begingroup$ Okay well here's what you do. To calculate the floor of a number, you just find the biggest integer that is less than or equal to that number. Like I did above. Can you guess what $\lfloor -3.14\rfloor$ is? No it is not quite rounding to the nearest integer. $\endgroup$ – Cameron Williams Nov 26 '13 at 1:04
  • 4
    $\begingroup$ @CameronWilliams I have 8 children coming over to paint two easter eggs each. A carton of eggs contains a dozen (12). How many cartons of eggs do I need? $\endgroup$ – RghtHndSd Nov 26 '13 at 1:37
3
$\begingroup$

Simply put it's two ways of thinking of rounding off a number. Ceiling rounds up to nearest integer. Floor rounds down to nearest integer. If the number is an integer, nothing happens.

$\endgroup$
2
$\begingroup$

It's easy to think about floor and ceil from the perspective of the number line. Let's say you have some decimal number, $2.31$ (I'm going to be using this number as an example throughout my answer) $\hskip2in$numberline
So, as you can see, the functions just return the nearest integer values.
floor returns the nearest lowest integer and ceil returns the nearest highest integer.

All real numbers are made of a characteristic (an integer part) and mantissa (a fractional part) $$\text{Number} = \text{Characteristic} + \text{Mantissa}$$ $$2.31 = 2 + 0.31$$

When floor a number, you can think of it as replacing the Mantissa with $0$ $$\lfloor 2.31 \rfloor = 2 + 0 = 2$$

and ceil can be thought of as replacing the mantissa with $1$. $$\lceil 2.31 \rceil = 2 + 1 = 3$$

That's not a very popular way of thinking about it but it was the way I thought about it when I first started using it in programming.

Remember, the number remains the same when it is an integer. ie, floor($3$) $=$ ceil($3$) $= 3$


Let's now look at the proper definitions along with the graphs for them.

Floor Function: Returns the greatest integer that is less than or equal to $x$ $\hskip2in$floor function

Ceiling Function: Returns the least integer that is greater than or equal to $x$ $\hskip2in$ceil function

Don't let the infinite staircase scare you. It's much more simpler than it seems. Those "line-segments" that you see are actually called piecewise-step functions.

Simply, the black dot represents 'including this number' and the white represents 'excluding this number'. Meaning that each segment actually is from x to all numbers less than x+1.


Let's look at 2.31 and how it would look on both the graphs at once. Graph of 2.31 hitting the floor and ceil functions You can see that the line $x=2.31$ hits the floor function at the "line-piece" for $2$ and hits the ceiling function at $3$

$\endgroup$
1
$\begingroup$

Here's an example of using the floor function: I have a number $m$ and I am looking for the smallest number $\ell$ such that $2^\ell\gt m+1$. To find such a number I would use the expression

$$\ell =[\log_2(m+1)]+1$$

Where the square brackets indicate the use of the floor function.

The floor and ceil functions in a programming language might use a combination of truncation (rounding towards zero) and comparisons to actually achieve their result. For example, if int is a function that truncates a number to its integer part, then for $x\ge0$ we have floor(x)=int(x), but for $x\lt 0$ it would be

y=int(x)
if (y\ne x)
  floor(x)=y-1
else
  floor(x)=y
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.