Python

Python Math Log

In Maths, log means inverse function to the exponent. The two numbers: one is the base, and the other is the exponent. For example, 2 is the base, and 4 is the power or exponent 2^4, equal to 16. When we take the log of 16 with a base value of 2, we get an answer of 4.

In Python, we use a different method to solve mathematical problems: the log() function. The math.log() function uses a math library to calculate the natural log of a specific number. It takes 2 values: including number and base (optional), to calculate the natural logarithm.

Example 01:

In example 1, we find the log of value with the base like log(x,(base)). This function uses two parameters: the number that is x, and the other is the base. It calculates the log of the base of argument x, expressed as log(x) / log(base).

import math
print ("logarithm of 17 is : ", end="")
print (math.log(17)
# Printing the log base 3 of 8
print ("Logarithm base 3 of 8 is : ", end="")
print (math.log(8,3))

Screenshot (193)

As we can see, the result of the math.log() method.

Screenshot (195)

Example 02:

In Example 2, we take a number as input from the user and store it in “number” variable and take another value(base) as input from the user stored in its variable “x”. Pass number and x value in the parameter of math.log() and get output through a print statement in python language.

import math
#Take input from user
 number = int(input("Enter the number:"))
 x = int(input("Enter the base:"))
print("log of number is:", math.log(number))
print("log of number is:", math.log(number, x))

Screenshot (197)

When we execute the code, we get the following result:

Screenshot (198)

Example 03:

In Logarithm with a custom base example, we created a function called logarithm() and passed base value and variable as a number in its parameter. Inside the function, we have initialized two variables, “x” and “y,” that store the math.log() method and return the variable. Print the function result as logarithm(x,y) in the last step.

import math
def logarithm(base,number):
    x = math.log(number)
    y = math.log(base)
return x/
# log with custom base:  
#                 base    number
print(logarithm(  4 ,   17    )  )
print(logarithm(  2 ,   100   )  )

Screenshot (200)

This example shows the result of the above-written code.
Screenshot (201)

Example 04:

In Example 4, we have implemented an if / else statement to check if the number is negative or not. We never get math domain errors due to condition statements.

The value error: MathDomain error comes when we pass zero or negative numbers, which can’t be calculated.

If we calculate the log of zero, math domain errors also occur.

import math
value = -23
if int(value) >= 0:
    math.log(value)
else:
print("We can't calculate the negative logrithm value")

Screenshot (204)

Output is presented here:

Screenshot (206)

Another code example without if-else condition to show the math domain error:

import math

value = 0

math.log(value)

Screenshot (208)

Output is presented here:

Screenshot (209)

Example 05:

The numpy.log() method is a mathematical feature that allows you to calculate a log, where a is the sum of the number in the input array.The inverse of exp() is the log, so log(exp(a)) = a. In Example 5, we initialize the variable “x” and store the 5 index array in it. Then initialize another variable “y” and store the value of function np.log(x).

import numpy as nump
x = [7, 11, 17, 3**9]
print ("Array with different values : ", x)
y = numpy.log(x)
print ("Result, "y)

Screenshot (211)

We will get the result after execution:

Screenshot (212)

Example 06:

The log1p() method is a built-in Python function that takes the natural log of 1+x (base x) when given a value. It takes a value and returns the natural log 1+value on the base x.

When we enter another value other than a number the function return a TypeError that indicate “a float is required”

In example 6, we take the input value from the user; then, we compute the logarithm(1+a) of 13.

import math
#  positive integer
number = int(input("Enter number: "))
#  Log(1+x)
print("Log of the value is: ", math.log1p(number))

Screenshot (214)

After the execution of this code, the result is given underneath.

Screenshot (215)

Example 07:

The function uses one value as a parameter and computes the log having base 2. The reason for using the base 2 value is we get a more accurate result, and if we pass no parameter in the function and the Type error message is displayed. We solve the below example with base value 2.

We print two statements, one depicts which function contains parameter value, we get the required result through this statement, and the other states which function doesn’t contain parameter value (no argument pass). So, in that case, we get the Type error message.

import math

print("Log of the value is: ",math.log2(37))

print("Log of the value is: ",math.log2())

Screenshot (219)

When we executed the program, the two different results appeared that are explained briefly in the upper paragraph.

Screenshot (220)

Screenshot (222)

Conclusion:

In the math function, we have studied many distinct log techniques. If we use any log functions with a negative value, we will get a math domain error. It contains a value of the log method and uses a library  “math”, allowing us to calculate logs with just one code line. In the first example, we have simply passed the value as a parameter in math.log() and gotten a simple result. In the second example, we take input from the user in compile time to get our desired result. In example 3, we use the function for checking how custom bases work in the python program. In the fourth example, we have used the condition statement to overcome the error problem and replace it with the if/else condition. In example 5, we have used numpy.log() to check how arrays work with logarithms. In the sixth example, we checked the natural logarithm with function log1p() equal to log(1+x). In the last example, we have used math.log2() function. We hope you will understand the python log function from all the above-explained examples.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content