📜  Python中的日志函数

📅  最后修改于: 2022-05-13 01:55:04.585000             🧑  作者: Mango

Python中的日志函数

Python在“数学”模块下提供了许多内置对数函数,它允许我们使用单行计算日志。对数函数有 4 种变体,本文将讨论所有这些变体。
1. log(a,(Base)) :该函数用于计算a的自然对数(以e为底)。如果传递了 2 个参数,它会计算参数 a 的所需基数的对数,即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
# Python code to demonstrate the working of
# log(a,Base)
 
import math
 
# Printing the log base e of 14
print ("Natural logarithm of 14 is : ", end="")
print (math.log(14))
 
# Printing the log base 5 of 14
print ("Logarithm base 5 of 14 is : ", end="")
print (math.log(14,5))


Python3
# Python code to demonstrate the working of
# log2(a)
 
import math
 
# Printing the log base 2 of 14
print ("Logarithm base 2 of 14 is : ", end="")
print (math.log2(14))


Python3
# Python code to demonstrate the working of
# log10(a)
 
import math
 
# Printing the log base 10 of 14
print ("Logarithm base 10 of 14 is : ", end="")
print (math.log10(14))


Python3
# Python code to demonstrate the working of
# log1p(a)
 
import math
 
# Printing the log(1+a) of 14
print ("Logarithm(1+a) value of 14 is : ", end="")
print (math.log1p(14))


Python3
# Python code to demonstrate the Exception of
# log(a)
 
import math
 
# Printing the log(a) of -14
# Throws Exception
print ("log(a) value of -14 is : ", end="")
print (math.log(-14))


Python3
# Python code to demonstrate the Application of
# log10(a)
 
import math
 
# Printing no. of  digits in 73293
print ("The number of digits in 73293 are : ", end="")
print (int(math.log10(73293) + 1))


输出 :

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

2. log2(a) :该函数用于计算 a 的以2 为底的对数。显示比 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

# Python code to demonstrate the working of
# log2(a)
 
import math
 
# Printing the log base 2 of 14
print ("Logarithm base 2 of 14 is : ", end="")
print (math.log2(14))

输出 :

Logarithm base 2 of 14 is : 3.807354922057604

3. log10(a) :该函数用于计算 a 的以10 为底的对数。显示比 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

# Python code to demonstrate the working of
# log10(a)
 
import math
 
# Printing the log base 10 of 14
print ("Logarithm base 10 of 14 is : ", end="")
print (math.log10(14))

输出 :

Logarithm base 10 of 14 is : 1.146128035678238

3. log1p(a) :该函数用于计算对数(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

# Python code to demonstrate the working of
# log1p(a)
 
import math
 
# Printing the log(1+a) of 14
print ("Logarithm(1+a) value of 14 is : ", end="")
print (math.log1p(14))

输出 :

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

1. ValueError :如果数字为负数,此函数返回值错误。

Python3

# Python code to demonstrate the Exception of
# log(a)
 
import math
 
# Printing the log(a) of -14
# Throws Exception
print ("log(a) value of -14 is : ", end="")
print (math.log(-14))

输出 :

log(a) value of -14 is : 

运行时错误:

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

log10()函数的应用之一是它用于计算编号。一个数字的位数。下面的代码说明了相同的情况。

Python3

# Python code to demonstrate the Application of
# log10(a)
 
import math
 
# Printing no. of  digits in 73293
print ("The number of digits in 73293 are : ", end="")
print (int(math.log10(73293) + 1))

输出 :

The number of digits in 73293 are : 5