# Floor and ceiling functions

Floor and ceiling functions are very cool functions that will allow us to do an even cooler thing at the end of this post! Keep reading to learn about them and find out what we’re going to do!

Floor function

The floor function of a real number gives as output the integer part of that number and is written as

$\text{floor}(x)\text{&space;or&space;}\left&space;\lfloor&space;x&space;\right&space;\rfloor$

Examples:

$\left&space;\lfloor&space;2&space;\right&space;\rfloor=2$ $\left&space;\lfloor&space;3.1&space;\right&space;\rfloor=3$ $\left&space;\lfloor&space;8.7&space;\right&space;\rfloor=8$

This is how it works for positive numbers; but what about negative numbers? It’s actually the same thing, except it may not be very intuitive:

$\left&space;\lfloor&space;-6&space;\right&space;\rfloor=-6$

but

$\left&space;\lfloor&space;-6.5&space;\right&space;\rfloor=-7$

vs

$\left&space;\lfloor&space;6.5&space;\right&space;\rfloor=6$

This is because the output of the floor function of a non-integer number is smaller than the input, like here: floor(4.7)=4. So it must be the same for negative numbers: floor(-6.5)=-7; -7 is smaller than 6.5.

Now, try to solve these two expressions:

$\left&space;\lfloor&space;4.6-9+8.9&space;\right&space;\rfloor=?$ $\left&space;\lfloor&space;3\cdot&space;2.3+5-16.8&space;\right&space;\rfloor=?$

Ceiling function

The ceiling function of a real number rounds it to the next integer, e.g. 4.7->5, 3.2->4 etc… . The result is the same if the input is an integer, like the floor function: 5->5. It is written as

$\text{ceil}(x)\text{&space;or&space;}\left&space;\lceil&space;x&space;\right&space;\rceil$

Examples:

$\left&space;\lceil&space;6.3&space;\right&space;\rceil=7$ $\left&space;\lceil&space;2&space;\right&space;\rceil=2$ $\left&space;\lceil&space;12.0001&space;\right&space;\rceil=13$

As you can see, even if the number is slightly larger than 12, the result is 13.

In case of negative numbers, we have:

$\left&space;\lceil&space;-5.9&space;\right&space;\rceil=-5$ $\left&space;\lceil&space;-3.2&space;\right&space;\rceil=-3$ $\left&space;\lceil&space;-7&space;\right&space;\rceil=-7$

I am pretty sure you were expecting the last result but not the first two; it was the same for me! I thought they would be -4 and -2 but here’s why the results are actually -5 and -3:

the ceiling function can be defined as

$\left&space;\lceil&space;n&space;\right&space;\rceil=\left&space;\lfloor&space;n&space;\right&space;\rfloor+1&space;\,&space;\,&space;\,&space;\forall&space;\,&space;n\notin&space;\mathbb{Z}$

i.e. it is valid for all of the non-integer values of n.

This is quite intuitive, I actually came up with it. It also means that n must not be equal to floor(n), since if it is, it means that n is an integer.

Notice:

As you can see, ceil(n) = floor(n) + 1. This means that ceil(-5.9) = floor(-5.9) + 1 = -6 + 1 = -5. This is why it is not -4 nor -6 nor whatever.

Number of digits

And now the cool part: with the following expression we will be able to get as result the number of digits of any integer!

$\text{number&space;of&space;digits}=\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor+1$

It can also be written in terms of the ceiling function using the definition we saw above:

$\left&space;\lceil&space;\log_{10}n&space;\right&space;\rceil=\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor+1\rightarrow&space;\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor=\left&space;\lceil&space;\log_{10}n&space;\right&space;\rceil-1$ $\left\{\,\begin{matrix}&space;\text{number&space;of&space;digits}=\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor+1&space;\\\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor=\left&space;\lceil&space;\log_{10}n&space;\right&space;\rceil-1&space;\end{matrix}\right.$

Therefore:

$\text{number&space;of&space;digits}=\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor-1+1=\left&space;\lfloor&space;\log_{10}n&space;\right&space;\rfloor$

These two functions have more complex applications as well, but I will talk about them in another post.

Hope you liked this lesson! If you have any doubts, leave a comment and I’ll be happy to help!

Join 31 other subscribers.

Support my site!

I put all of my efforts to make this site as good as possible. If you would like, you can help my site grow with a small donation! I would really appreciate it.

\$1.00

### 1 Comment »

1. Vince

Thank you so much