Arithmetic Functions: Rounding
00:00
Rounding: rounding floats to ints. Let’s see the functions in the math
module that do this.
00:07
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 x
.
00:23
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 x
.
00:39
Next is the floor()
function. 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 x
.
00:53
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.
01:08
And then, lastly, we’ve got trunc()
, which is short for truncation, and trunc(x)
returns the integer part of x
.
01:16
So if 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.
01:29
The rounding function trunc()
is a composition of floor()
and 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.
01:47
So if 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.
02:09
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 6
.
02:28
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 -12.3
is -12
.
02:43
Now let’s use the floor()
function with these same numbers. 5.43
, floor()
rounds to the left, so we get 5
.
02:54
And if we round -12.3
with 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 -12
.
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.
03:49
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.