In mathematical and scientific applications, the “Natural Logarithm” is very useful due to its several properties. This logarithm is considered the inverse function of the natural exponential function, such as “f(x) = e^x”. To determine the natural log of the given number different methods are utilized in Python. But for this guide, we will focus only on the “math.log()” method.
This blog demonstrates the below contents:
- What is the Natural Logarithm?
- What is the “log()” Method in Python?
- Calculate the Natural Logarithm of a Number
- Retrieving “ValueError” and “TypeError”
- Difference Between “math.log()” and “math.log10()” Method
What is the Natural Logarithm?
The “Natural Logarithm” denoted as “len(x)” or “log(x)” is a mathematical function that represents the logarithm on the base of the mathematical constant “e”. The number “e”, also referred to as Euler’s number, is an irrational and transcendental constant roughly equal to “2.71828”. The natural logarithm of “e” itself, “ln(e)” is “1”, because “e^1=e”, while the natural logarithm of “1” is “0”.
What is the “math.log()” Method in Python?
In Python, the “math.log()” method is used to determine the natural logarithm of a number or the number logarithm to a particular base. The logarithm to the base “e” or Euler’s number is referred to as a natural logarithm.
Syntax
Parameters
In the above syntax:
- “x” parameter: The value used to determine the logarithm.
- “base” parameter: This optional parameter represents the logarithm base to use. The default is set/assigned to “e”.
Return Value
The “math.log()” method retrieves the float value representing the natural logarithm or the logarithm to a base of the given number. This method retrieves “ValueError” if the value is a negative number or zero. It will also retrieve the TypeError if the value is non-numerical.
Example 1: Calculate the Natural Logarithm of a Number/Value
In this code, the “math” module is imported, and the “math.log()” function determines the natural logarithm of the given number:
print(math.log(3.7183))
print(math.log(3))
print(math.log(2))
The below snippet retrieves the natural logarithmic of the input numbers:
Example 2: Retrieving “ValueError” and “TypeError”
In this code, the “math.log()” takes the negative number and “zero” as an argument to retrieve the ValueError:
print(math.log(-3.7183))
print(math.log(0))
The above code displays the ValueError to the console:
We can not pass the strings or any non-numerical value to the “math.log()” method. Take this code to retrieve the TypeError:
print(math.log('3.7183'))
The code retrieves a TypeError:
Example 3: Difference Between “math.log()” and “math.log10()” Method
The main difference between “math.log()” and “math.log10()” methods is the base of the logarithm they determine. The “math.log()” method determines the number of logarithms to the particular base. If the base is not provided, it defaults to the natural logarithm with base “e”.
While on the other hand, the “math.log10()” method determines the logarithm of a number to the base 10. It is created to be more accurate than using “math.log()” for base-10. Let’s understand it via the example:
x = 100
print('Base-10 Logarithm Using math.log10(): ', math.log10(x))
print('\nBase-10 Logarithm Using math.log(): ', math.log(x, 10))
print('\nNatural Logarithm Using math.log(): ', math.log(x))
Here, we can determine the base-10 logarithm using the “math.log()” and “math.log10()” method which retrieves the same output. We can also determine the natural logarithm of the given number without passing the base parameter.
The code retrieves the following outcome:
Conclusion
The “math.log()” method of the “math” module is used to determine the natural logarithm of the specified number in Python. This method determines the natural logarithm by taking the base value “e” as the default. The difference between the “math.log()” and “math.log10()” methods is also discussed in this blog.