Arithmetic Functions: Rounding
Rounding: rounding floats to ints. Let’s see the functions in the
math module that do this.
There are three functions in the
math module that round a float to the nearest integer. The first one we’ll take a look at is
ceil(), which is short for ceiling, and
ceil(x) returns the nearest integer greater than or equal to
If you think of the real numbers as points on a number line, when you take
ceil(x), this will round to the right, and so
ceil(x) will return the integer that’s closest to the
x but on the right of
Next is the
floor(x) returns the nearest integer less than or equal to
x. So on the number line,
floor(x) is going to be the integer that is to the left of
x and closest to
Now, this can get a little tricky when you are dealing with negative numbers, and we’ll take a look at some examples, but if you keep the analogy of the real numbers as being points on the number line,
floor(x) rounds to the left on that number line.
And then, lastly, we’ve got
trunc(), which is short for truncation, and
trunc(x) returns the integer part of
x is a float and it’s got digits after the decimal point, we drop all those decimal points and keep the integer part. If we think of
x as being a point on the number line, we are rounding towards zero.
The rounding function
trunc() is a composition of
ceil(). Let me show you what I mean. Because
trunc() rounds towards zero, we can write
trunc(x) in terms of floor and ceiling depending on whether
x is greater or equal to zero or whether it’s negative.
x is a positive number—a positive float—then
trunc(x) will be rounding to the left, and so that’s equal to
floor(x). But if
x is negative, if we want to round towards zero, what we want to do is round to the right, and that’s what ceiling of
x does. So to clarify everything, let’s just look at some examples.
Let’s start off with
ceil(). Round to the right the number
5.43. Another way to think about it is that we’re rounding up. But again, think of the numbers as being the numbers on the number line. Rounding to the right with
ceil() gives us the integer
If instead we use a negative number, so,
-12.3, here we’re rounding with
ceil(), so we’re rounding to the right, and the nearest integer to the right of
Now let’s use the
floor() function with these same numbers.
floor() rounds to the left, so we get
And if we round
floor(), rounding to the left gives us
-13. Now let’s use
trunc(). The truncation of
5.43, this will get rid of the decimal numbers
.43, and so we get
5. And if we do this with the negative number
-12.3, we’re getting rid of the
.3 and you get
03:23 Let’s confirm the formula that you saw for the truncation function.
03:29 When the input value is a positive number, that is going to equal the floor of the number.
03:38 And when the input value is negative,
03:41 the truncation of that number is going to equal the ceiling of the number.
So, those are the functions in the
math module that take an input number and round to the nearest integer. In the next lesson, we’ll take a look at a function in the
math module to help you decide when two floats are close. “How close?” depends on your relative tolerance.
Become a Member to join the conversation.