Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Rounding Half Up and Half Down

00:00 In the previous lesson, I showed you how to measure the bias of a rounding function. In this lesson, I’ll show you two new ways to round that do a less biased job.

00:09 Spoiler alert, I did not say unbiased. Our previous attempts at implementing a rounding function had some pretty bad bias in them. Changing the shape of the data being rounded, always rounding up or always rounding down isn’t such a good idea.

00:25 So if you’re thinking back to the number line in the previous lesson, what you’re looking for is everything to the left of the midway point rounds down everything to the right of the midway point rounds up, and then the question is what to do with that midway point.

00:41 Here are two strategies that use a similar approach. Rounding half up adds 0.5 and takes the floor. This might seem a little counterintuitive, but by adding 0.5, anything to the right of the midway point becomes the number bigger.

00:57 While adding 0.5 to values that are left of the midway will end up having the same integer component. Taking the floor of the result means you’ve split the difference.

01:09 Another strategy is rounding half down. This takes the other approach. It subtracts 0.5 and then takes the ceiling instead of the floor. Same idea, but pulling in the other direction.

01:23 Consider 13.825 in our first three cases of tens, ones and tenths, both methods return the same result, which is more or less our grade school style rule.

01:37 The hundredths is where it gets interesting. This is exactly the midway point, so you’re in tiebreaker land and our two strategies result in two different things.

01:46 One rounds the tie up, the other rounds down. Before I head into the REPL, think for a moment about whether or not these methods are biased and if so, are they better or worse than what you’ve seen so far?

02:01 I’m back in rounding.py and here are my two new methods. The key part of the half up approach is to add 0.5 after shifting the number, then taking the floor.

02:13 As I said earlier, adding 0.5 increases the ones digit of the shifted number by one if the number is past the midway point, then calling floor gives you the result of rounded up or down and like our previous functions, you then shift the number back to its original magnitude.

02:33 Half down uses the same kind of math trick but in the opposite direction by subtracting and then taking the ceiling. This either lowers the ones by a digit or not and then ceiling gives you the next number up.

02:47 Let’s try this out. Importing half up

02:56 and here’s 13.825, rounded half up, importing half down,

03:10 and there you go in the other direction. Now what about the bias here? Well, let me grab the numbers list from the previous lesson

03:22 and a reminder of the mean.

03:29 Let’s use half up on the numbers

03:37 and the associated mean.

03:46 That’s pretty close. In fact, it’s the same as what truncate() did for us in the previous lesson. And let’s do the same with half down

03:59 and the mean.

04:08 Not quite as good, but far better than before.

04:13 The two new approaches are better than simply rounding up or rounding down indiscriminately. If you’ve got no ties these are unbiased, but that’s a big if.

04:25 How biased they are comes down to the data. Since both algorithms always tie break in a specific direction, anytime you round something that is exactly midway, half up and half down, always do the same thing thus shifting your data’s shape a bit.

04:42 The result is that neither of these are quite symmetric around zero, and although that’s not as extreme as purely rounding up or down, the handling of positive and negative numbers will still shift your data.

04:56 In the next lesson, I’ll finish off the bias once and for all, almost.

Become a Member to join the conversation.