Python在模块下提供了许多内置对数函数” 数学 “这使我们能够使用一行代码计算日志。对数函数有4种变体,本文将对它们进行讨论。 1.日志(a,(基础)): 此函数用于计算 自然对数 (基数e)a。如果传递了两个参数,它将计算参数a所需基数的对数,数值为 日志(a)/日志(基本) .
null
Syntax :math.log(a,Base)Parameters : a : The numeric valueBase : Base to which the logarithm has to be computed.Return Value : Returns natural log if 1 argument is passed and log withspecified 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 )) |
输出:
Natural logarithm of 14 is : 2.6390573296152584Logarithm base 5 of 14 is : 1.6397385131955606
2.log2(a): 此函数用于计算 对数底2 显示的结果比日志(a,2)更准确。
Syntax :math.log2(a)Parameters : a : The numeric valueReturn Value : Returns logarithm base 2 of aExceptions : 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): 此函数用于计算 以10为底的对数 a.显示的结果比log(a,10)更准确。
Syntax :math.log10(a)Parameters : a : The numeric valueReturn Value : Returns logarithm base 10 of aExceptions : 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 valueReturn 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.值错误: 如果数字为,此函数将返回值错误 消极的 .
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
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END