Change language

Log functions in Python

| |

1. log (a, (Base)): this function is used to calculate the natural logarithm (Base e) for a. If 2 arguments are passed, it computes the logarithm of the required base of argument a, the numerical value log (a) / log (Base) .

  Syntax:  math. log (a, Base)  Parameters:   a:  The numeric value  Base:  Base to which the logarithm has to be computed.  Return Value:  Returns natural log if 1 argument is passed and log with specified base if 2 arguments are passed.  Exceptions:  Raises ValueError is a negative no. is passed as argument. 

# Python code to demonstrate how it works
# log (a, Base)

 

import math

 
# Print database of 14 e logs

print ( "Natural logarithm of 14 is:" , end = "")

print (math.log ( 14 ))

 
# Print database 5 of 14 log

print ( " Logarithm base 5 of 14 is: " , end = "")

print ( math.log ( 14 , 5 ))

Output:

 Natural logarithm of 14 is: 2.6390573296152584 Logarithm base 5 of 14 is: 1.6397385131955606 

2. log2 (a): this function is used to calculate the log base 2 for a. Displays a more accurate result than log (a, 2).

  Syntax:  math.log2 (a)  Parameters:   a:  The numeric value  Return Value:  Returns logarithm base 2 of a  Exceptions:  Raises ValueError is a negative no. is passed as argument. 

# Python code to demonstrate how it works
# log2 (a)

 

import math

 
# Print database 2 of 14 log

print ( "Logarithm base 2 of 14 is:" , end = "")

print (math.log2 ( 14 ))

Output:

 Logarithm base 2 of 14 is: 3.807354922057604 

3. log10 (a): this function is used to calculate the log base 10 for a. Displays a more accurate result than log (a, 10).

  Syntax:  math.log10 (a)  Parameters:   a:  The numeric value  Return Value:  Returns logarithm base 10 of a  Exceptions:  Raises ValueError is a negative no. is passed as argument. 

# Python code to demonstrate how it works
# log10 (a)

 

import math

 
# Print base 10 of 14 log

print ( "Logarithm base 10 of 14 is:" , end = "")

print (math.log10 ( 14 ))

Output:

 Logarithm base 10 of 14 is: 1.146128035678238 

3. log1p (a): this function is used to calculate the logarithm (1 + a) .

  Syntax:  math.log1p (a)  Parameters:   a:  The numeric value  Return Value:  Returns log (1 + a)  Exceptions:  Raises ValueError is a negative no. is passed as argument. 

# Python code to demonstrate how it works
# log1p (a)

 

import math

 
# Print magazine (1 + a) out of 14

print ( "Logarithm (1 + a) value of 14 is: " , end = " ")

print (math.log1p ( 14 ))

Output:

 Logarithm (1 + a) value of 14 is: 2.70805020110221 

exception

1. ValueError: This function returns a value error if the number is negative .

# Python code to demonstrate the exception
# log (a)

 

import math

 
# Print log (s) -14
# Throws an exception

print ( "log (a) value of -14 is:" , end = "")

print (math.log ( - 14 ))

Output:

 log (a) value of -14 is: 

Runtime error:

 Traceback (most recent call last): File "/home/8a74e9d7e5adfdb902ab15712cbaafe2.py", line 9, in print (math.log ( -14)) ValueError: math domain error 

Practical use

One use of the log10 () function is that it is used to calculations no. digits of the number . The code below illustrates the same.

# Python code to demonstrate usage
# log10 (a)

 

import math

 
# Print # of digits in 73293

print ( "The number of digits in 73293 are: " , end = " ")

print ( int (math.log10 ( 73293 ) + 1 ))

Output:

 The number of digits in 73293 are: 5 

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method