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

Unlock This Lesson

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

Unlock This Lesson

Other Important math Functions: fsum(), sqrt(), and More

00:00 I hope you took a break, maybe got something to drink. So, let’s keep going with the functions in the math module. I promise, in the next lesson, we’ll take a look at some of the newer ones that were added in Python 3.8.

00:13 So, let’s start with the fsum() function. This is very similar to the sum() function in Python, but let me show you the example that’s used in the documentation—why you may want to use the fsum() function. So, let’s take the number 0.1 and we want 10 of those in a list, and we want to sum those up.

00:32 So, 0.1 ten times should be 1. Let’s see what the sum() function returns.

00:40 So we get something very close to 1, but, you know, a little bit unsettling that we don’t get exactly 1.0. If, instead, we use the fsum() function in the math module, then we do get exactly 1.0. In the documentation for the fsum() function, it states that by tracking multiple intermediate partial sums, it’s able to avoid loss of precision. And so, for example, if you were to add up 0.1, 0.1, 0.1, and maybe, say, four.

01:14 And then if you then added up 0.4 and 0.4, and then maybe 0.1 and 0.1, then you would get exactly 1.0.

01:24 So basically, it uses a more optimal algorithm to add up floating-point numbers, instead of just sort of doing them one by one.

01:34 Let’s try, say, the sqrt() (square root) function. So, what is the square root of 20,457,529?

01:48 I’m going to guess about 4,523? Yeah, it looks like I got that right! So, again, the sqrt() function just computes the square root of a float, it doesn’t have to be an integer, of course. Say, 20.35.

02:05 So, the only thing, as with a lot of the math module functions—in fact, all of the math module functions—they don’t deal with complex numbers.

02:12 So if you tried something like sqrt(-88.5), you’re going to get a ValueError. The error is that this is a math domain error.

02:23 We’ve seen this ValueError message before for a lot of the math module functions. Whenever the function you’re using, if it has a limitation on the domain, then you’re going to get this math domain error message.

02:39 All right, what’s up next? We’ve got degrees and radians. The basic idea is, the full circle measures 360 degrees and that’s equivalent to 2π radians. So, for example, if you wanted to convert 180 degrees, which is halfway around the circle… If the full circle is 2π radians, half the circle is just π radians.

03:05 So that means that 180 degrees, which is half of 360, should be π, and so that’s what we get. So, radians() allows you to convert from degrees to radians.

03:18 And then if you want to go the other way, say, from radians to degrees… So, for example, π radians is 180 degrees. Or, say π over 2, so that is 90.0.

03:32 And so on. π over 4? 45 degrees.

03:38 Then we’ve got the classical trig functions. Say, what is sine of π divided by 2? 1.0. Whereas the cosine of π over 2 is zero, which gives us very close to zero floating-point approximation.

03:58 All right, so that’s a quick overview of some of the other functions in the math module that you may be interested in. Coming up next, we’ll take a look at some of the functions that were added to the math module in Python 3.8.

Become a Member to join the conversation.