What does math log mean in python?

Python offers many inbuild logarithmic functions under the module “math” which allows us to compute logs using a single line. There are 4 variants of logarithmic functions, all of which are discussed in this article.
1. log[a,[Base]] : This function is used to compute the natural logarithm [Base e] of a. If 2 arguments are passed, it computes the logarithm of the desired base of argument a, numerically value of 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 if a negative no. is passed as argument.

Python3

import math

print ["Natural logarithm of 14 is : ", end=""]

print [math.log[14]]

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 compute the logarithm base 2 of a. Displays 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 if a negative no. is passed as argument.

Python3

import math

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 compute the logarithm base 10 of a. Displays 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 if a negative no. is passed as argument.

Python3

import math

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 compute logarithm[1+a]

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

Python3

import math

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 value error if number is negative

Python3

import math

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 Application

One of the application of log10[] function is that it is used to compute the no. of digits of a number. Code below illustrates the same.

Python3

import math

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 is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


What does math log do?

The Math. log[] method returns the natural logarithm [base E] of a number.

How do you write logs in Python?

Adding logging to your Python program is as easy as this:.
import logging..
import logging logging. debug['This is a debug message'] logging. info['This is an info message'] logging. ... .
WARNING:root:This is a warning message ERROR:root:This is an error message CRITICAL:root:This is a critical message..

What base is log in Python?

Understanding the log[] functions in Python The math. log[x] function is used to calculate the natural logarithmic value i.e. log to the base e [Euler's number] which is about 2.71828, of the parameter value [numeric expression], passed to it.

Chủ Đề