The Log Function Of Python Math Module

Overview:

The function returns the logarithm of a given number for a given base. When base is not given the function uses e as the base and returns the natural logarithm.

Example:

# Example Python program that uses
# the log function to compute the logarithm
# of numbers for various bases
import math 

num     = 1000000
base     = 10
logVal     = math.log(num, base)
print("Logarithm of %d base %d : %d"%(num, base, logVal))

base     = 5
logVal     = math.log(num, base)
print("Logarithm of %d base %d : %d"%(num, base, logVal))

base     = 2
logVal     = math.log(num, base)
print("Logarithm of %d base %d : %d"%(num, base, logVal))

logVal     = math.log(num)
print("Natural logarithm of %d : %d"%(num, logVal))

Output:

Logarithm of 1000000 base 10 : 5

Logarithm of 1000000 base 5 : 8

Logarithm of 1000000 base 2 : 19

Natural logarithm of 1000000 : 13

 


Copyright 2022 © pythontic.com