Logarithmic Functions: math.log()
00:00
Usually, after you study exponential functions, you move on to logarithmic functions, and this is what we’ll do in this lesson. In particular, we’ll take a look at the logarithm function defined in the math
module.
00:13 Okay, so you made it this far. You got through the radioactive decay problem, and so now you’re in for a treat because we’re going to take a look at logarithmic functions. I know, I know, settle down. Let’s get through this.
00:26 So, here’s how these functions come up. Say you have an exponential function 2.5 to the power of x, and you want to know for what value of input x will—when raising 2.5 to the power of that x—are you going to get 39.0625? If you think about it, this is an inverse problem.
00:46 You want to know “What x gives me this output?” This is where logarithmic functions come in. Every exponential function a to the x has an inverse.
00:58 The inverse of a to the x is the logarithm function with base a. These functions are denoted by log subscript a, the input is x.
01:07 The constant is a here—it’s fixed—and the thing that varies is x. So, what can you do with this logarithm function base a? Well, you can solve this inverse problem.
01:20 The logarithm with base 2.5 evaluated at the input 39.0625, we get 4. So that means that 2.5 raised to the power of 4 is 39.0625. So, using the logarithm with base 2.5 solves our inverse problem.
01:45
So, in Python, to compute the logarithm base a evaluated at x, we use the log()
function. It takes two inputs. The first one is x
and the second one is the base.
01:58
Both of these values x
and a
, they must be positive numbers.
02:04
Okay, one last thing before we get on to testing these functions. There are two other frequently used logarithm functions besides the natural logarithm. This is when a is equal to 2, so instead of calling math.log(x, 2)
, there’s the dedicated function log2()
in the math
module.
02:24
This is going to be more accurate than using log()
with x, 2
. And then the other one is when the base is a = 10, and instead of writing log(x, 10)
, there’s the dedicated function in the math
module called log10()
. And again, this one’s going to be more accurate than calling log()
with x
and then with a 10
for the base. So, now let’s test these functions in Python.
02:52
Let’s compute a couple of values with the logarithm function. The logarithm of 54
, say, base 3
.
03:02
As you saw, the logarithm functions—they are the inverse functions of the corresponding exponential functions. Let’s save this value in the variable x
, and we know that the logarithm with base 3
is the inverse function of the exponential function with base 3
. Now, if we computed that the logarithm of 54 base 3 is 3.63 and so on, and we’re saving that in the variable x
, that means then that 3
raised to the power of x
should be 54
. So, modulo some rounding error, we see that this is working well.
03:41 Logarithm functions are the inverses of the corresponding exponential functions.
03:47
Every exponential function—it doesn’t matter what the base is, you’re always going to get a positive number. That means that the only numbers you can input into the logarithm as the first argument, they must be positive. For example, if you try the logarithm , say, -54
base 3
, what you’re asking for here is “If the base is 3 and I raise 3 to the power of some number, what gives me -54?” and that’s what this would compute. But you get a domain error, a math domain error
, and that’s because there’s no number that you can raise 3 to the power of to get -54. You’re always going to get a positive number.
04:29
Let’s check out the couple of built-in special logarithm functions. These were math.log2()
, so this will compute the logarithm of whatever input with base 2.
04:40
So, for example, 32
—that’s 5
. So 2
to the power of 5
is 32
.
04:50
Now go ahead and try, say, using log10()
. That was the other special log function. Just for convenience, Python defines these two because they’re frequently used.
04:59
Go ahead and try, say, the logarithm of 1000
base 10
. So, what number do you need to raise 10 to the power of to get 1,000? That’s 3.
05:09
Now let’s compare the accuracy if instead of using log10()
, we used, say, log()
with the second argument passing in the base.
05:19
So in this case, we’re off a little bit again due to rounding, and so this is a good example of why you would want to use these specialized functions log2()
and log10()
.
05:28
They’re going to be more accurate than if you were to use the log()
function with the corresponding base. All right, so that is the basic usage of the logarithm function and the two specialized ones log2()
and log10()
. In the next lesson, we’ll take a look at the natural logarithm in an application using the decay problem with Californium-252.
Become a Member to join the conversation.