For more information about concepts covered in this lesson, you can check out:
Other Important math Functions: gcd()
There are a lot of other functions in the
math module. Let me just show you a few, and then in the next lesson, we’ll talk about some of the newer functions that appeared in Python 3.8.
The first function we’ll talk about is
gcd(). This is short for the Greatest Common Divisor, and it takes a variable number of integers and it computes the greatest common divisor of those numbers. Now, usually, you want to compute the greatest common divisor of two integers, but
gcd() actually takes either one integer or zero integers, In the case where zero integers are passed, then it returns
0, and if you just pass in one integer, it’s going to return that same integer.
Now, prior to Python version 3.9,
gcd() only took in two arguments, whereas starting with version 3.9, it does take an arbitrary number of arguments.
So just be aware of that in case you’re using a version of Python prior to 3.9. Next, we’ve got
fsum() takes an iterable as the argument, and it’s very similar to the
sum() function: It computes the sum of the numbers in the iterable, but we’ll see that it’s a little bit more accurate than the sum() function that’s built into Python. Then we’ve got
sqrt(), which is short for square root. this computes the square root of a number, and it has to be a positive number or
Otherwise, it’s going to return a
ValueError because the functions in the
math module only deal with real numbers and not complex numbers.
Then there are a couple of functions that convert from degrees to radians. The
degrees() function converts to degrees from radians, and the
radians() function converts to radians from degrees. In most computations with trigonometric functions, the inputs are interpreted as radians or are assumed to be radians, and so if you’re working with degrees, you’ll want to convert to radians.
And then we’ve got your favorite trig functions like cosine, sine, and tangent. These compute their values on the assumption that the input is in radians, and so, again, you may want to use the
radians() function to do that if you’re working initially with degrees.
And then you’ve got the inverses of the sine, cosine, and tangent functions. These are
acos()—that’s the arccosine of
asin()—that’s the inverse of sine, and then
atan()—that is the inverse or arctangent of
x. Now, I’ll mention this right here that the
asin(), they have domain limitations on the input.
These inputs can only be between
1, whereas for the
atan() function, the input
x can be any real number.
02:44 All right, let’s try a few of these.
Let’s start off with
gcd() function will compute the greatest common divisor of an arbitrary number of integers. If you don’t pass any integers, you’ll get zero. If you pass in one integer,
then you just get the integers back. And if you pass in two, then this is what you may have learned about the
gcd(), is that it finds the greatest common divisor of two integers. In this case, you get
Now let’s add another one, say,
Then you get
8. So this is kind of cool because if you have a list of integers and you want to find the greatest common divisor of all of the numbers in the list, then you can use the
And if you’re familiar with the unpacking operator (
*) in Python, then you can use that instead of having to do, say, some sort of recursion or a reducing type of operation. Let me show you what I mean.
03:50 Here’s a list of integers, and if you wanted to find the greatest common divisor of these numbers, you could just put these in one by one… Oh, this is kind of slow! Hey, why don’t we use the unpacking operator?
04:04 So, if you’re not familiar with it, there will be a link underneath the video description there for you to read up. But basically, if you have an iterable and you want to use those iterable elements, the elements in the iterable, as arguments to a function that takes on an arbitrary number or however many number of elements in the iterable, then you can use this star or unpacking operator.
And so that’s what we get,
8! So, this would have been the same if you had written these out one by one as arguments to the
So, that’s kind of cool with the
gcd(). Again, this is only starting with Python version 3.9.
Now let’s take a look at the
fsum() function. Actually, why don’t you grab a drink, come back, and then in the next lesson, we’ll finish off with some of the other functions in the
Become a Member to join the conversation.