Other Important math Functions: fsum(), sqrt(), and More
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.
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.
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, and maybe, say, four.
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,
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.
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.