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

Exploring Other Libraries

00:00 In the previous lesson, I explained how floating points are both impressive and problematic. In this lesson, I’ll point you at rounding capabilities in a few other Python libraries.

00:10 Python has a separate numeric class for precise representation. It’s the decimal class found in the decimal module. This one is precise, so it’s the best choice if you are coding stuff around money.

00:25 How precise? Well, it’s actually partly up to you. The decimal class uses a context object to define the precision, the rounding strategy, and more.

00:35 Let’s go to the REPL and I’ll give you a quick tour of the decimal class.

00:41 Let me import the module. The class in the module is named Decimal, capital D but if you’re doing a lot of math stuff with it, you have to keep typing that over and over again.

00:52 So I’m going to use a shortcut, which is I’m going to store the Decimal class in a new value called the letter D, and then I can just use that.

01:01 Remember, everything in Python is an object. Even the classes, this is not creating an instance. This is remapping the reference to the class.

01:12 Now, to create a decimal, I pass a number or a string with a number in it into the class. Why a string you might say, well, it goes back to the floating-point problem.

01:24 You can pass in 0.1, but that’s a float and it isn’t precise. Decimal converts the thing that it’s given, which is the problematic value, so you don’t actually end up with 0.1 purely in decimal.

01:36 You get 0.1 float converted into the wrong value. Hence, this is why you typically use strings to create new Decimal classes in order to keep your precision.

01:46 I mentioned before that the decimal module uses a context object to define how it works. You can get at that by calling the module’s get_context() function.

01:59 The default context has a precision of 28 decimal places and uses the half_even rounding strategy that you know and love. Let’s see how rounding works with this class.

02:13 As a quick reminder, here’s the problematic float

02:18 and its problematic rounding, and now let’s do that with decimals. Like with most numeric operations in Python, calling round() is actually triggering a special method on the class, whether that’s an int, a float, or decimal. round() calls the class’s __round__ special method.

02:37 This means the built-in round() function can be used with the decimal class just like it is with ints or floats.

02:48 Remember, I’ve used a string to make sure that I’ve got a precise number. Then rounding it gives the correct answer. Insert trumpet noises here.

02:59 Anyhow, by accessing the context, you can change the context’s values. The decimal module has several constants that define different rounding strategies.

03:15 Here, I’ve modified the decimal module to use a round_up strategy,

03:23 which of course changes the result. I’ll leave it as an exercise for you to go and dig into the documentation to find all the others, but everything you’ve seen in this course is in there.

03:36 If you’re doing the data science thing or playing with a lot of data, you might be using the third-party NumPy library. Never fear. It also has a round() method, which knows how to round all the data in NumPy arrays.

03:51 You might also be using pandas. The pandas series and DataFrame classes both have rounding methods as well. There’s lots of choices out there depending on what tools you’re using.

04:04 Before I finish the course up, just a couple quick recommendations. First, try to use as much precision in your data as space will allow for your application.

04:14 Second, don’t round until you have to. Leave it as late as possible so you don’t introduce imprecision by rounding your rounding. The IEEE 754 floating-points spec defaults to the rounding to even strategy, and for the most part, so should you.

04:32 This whole course could have been titled, Just use the built-in function, you’ll be fine. Money does add a little bit of extra complication. Not only do you need to worry about the floating-point precision problem, you also have to worry about countries rounding standards.

04:49 This Wikipedia page has a comprehensive list of the strategies used in different countries. For example, my home of Canada got rid of the penny a few years back.

04:58 The price of copper and the cost of inflation just kind of made them problematic. If you’re paying with a credit card, you pay every cent as normal. If you’re paying with cash, then there are rounding rules to bump up or down to the nickel depending on the price.

05:12 European countries that have this same thing don’t necessarily use the same rounding rule, so if you’re implementing code that is locale specific and using money, understand that the rounding rules might be different.

05:26 If you want to dig in hardcore to the rounding stuff, the Wikipedia page covers all the algorithms that I talked about here, as well as bias and plenty of other useful information.

05:37 That’s more or less the course. Like I said, just use the built-in rounding function. Last up, I’ll summarize everything and point you at some sources of interest.

Become a Member to join the conversation.