Python程序中的对数函数
在本教程中,我们将学习math模块中的对数函数。我们有四种类型的对数函数。Python的math模块提供了所有这些函数。让我们逐一学习它们。
math.log(number, [Base])
math.log(number, [Base]) 方法用于计算任何Base的对数。如果我们没有指定任何基数,则它将以e为默认基数。
注意 - 如果您向方法传递负数,则会得到ValueError。
示例
让我们看一些例子。
# importing math module import math # logarithm with base 3 print(math.log(15, 7))
输出
如果您运行上面的程序,您将得到以下结果。
1.3916625094004957
您可以在上面的程序中指定任何您想要的基数。让我们看看没有基数的相同示例。默认基数是e。
示例
# importing math module import math # logarithm with base e(default) print(math.log(15))
输出
如果您运行上面的代码,您将得到以下结果。
2.70805020110221
示例
让我们看看如果我们将负数传递给math.log()方法会发生什么。
# importing math module import math # logarithm with negative number print(math.log(-15))
输出
如果您运行上面的程序,您将得到以下结果。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-b686fcb806c6> in <module> 3 4 # logarithm with base e(default) ----> 5 print(math.log(-15)) ValueError: math domain error
math.log2(number)
如果您想计算以2为底的对数,则可以使用math.log2()方法。它与上述方法类似。让我们看一些例子。
示例
# importing math module import math # logarithm with base 2 print(math.log2(15))
输出
如果您运行上面的代码,您将得到以下结果。
3.9068905956085187
与math.log方法类似,如果我们将负数传递给math.log2方法,我们将得到错误。让我们用例子来看一下。
示例
# importing math module import math # logarithm with base 2 & negative number print(math.log2(-15))
输出
如果您通过执行程序查看程序的输出,您会发现现在和之前得到的错误是相同的。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-8019b45e571f> in <module> 3 4 # logarithm with base 2 & negative number ----> 5 print(math.log2(-15)) ValueError: math domain error
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
math.log10(number)
我们可以使用math.log10方法找到以10为底的对数。它与上面的math.log2方法类似。让我们看一些例子。
示例
# importing math module import math # logarithm with base 10 print(math.log10(15))
输出
如果您执行上面的程序,您将得到以下输出。
1.1760912590556813
尝试将负数传递给math.log10方法。您将得到与上述方法类似的错误。
示例
# importing math module import math # logarithm with base 10 & negative number print(math.log10(-15))
输出
如果您查看输出,您将得到以下错误。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-52ac56b802ca> in <module> 3 4 # logarithm with base 10 & negative number ----> 5 print(math.log10(-15)) ValueError: math domain error
math.log1p(number)
math.log1p(x)方法将计算以e为底的log(1 + x)。它通过向给定数字添加1来计算其对数。让我们看一些例子。
示例
# importing math module import math # logarithm print(math.log1p(15)) # similar to math.log(16)
输出
如果您执行上面的程序,您将得到以下结果。
2.772588722239781
尝试将负数传递给math.log1p方法。我相信您会得到与之前看到的相同的错误。
示例
# importing math module import math # logarithm print(math.log1p(-15))
# 导入math模块 import math # 对数 print(math.log1p(-15))
输出
由于我们将负数传递给了该方法,我们将得到以下错误。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-15-26016884cb23> in <module> 3 4 # logarithm ----> 5 print(math.log1p(-15)) ValueError: math domain error
结论
我们已经看到了math模块中的四个对数方法。如果我们将负数传递给本教程中看到的任何对数方法,我们将得到错误。您也可以将浮点数传递给这些方法。尝试使用浮点数执行本教程中看到的示例。