Logarithmic Functions: math.log()
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
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.
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.
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
This is going to be more accurate than using
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
x and then with a
10 for the base. So, now let’s test these functions in Python.
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.
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,
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.
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
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.