# 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

Examples:

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:

but

vs

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:

Leave the answer in the comments!

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

Examples:

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

In case of negative numbers, we have:

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

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:

n | floor(n) | ceil(n) |

0.52 | 0 | 1 |

8.87 | 8 | 9 |

14.37 | 14 | 15 |

22.79 | 22 | 23 |

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!

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

Therefore:

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!

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

Thank you so much