python数学模块函数
Python数学模块日志功能 (Python math module log functions)
In python programming language, there are some of the following log functions which are defined in math module,
在python编程语言中 , 数学模块中定义了以下某些日志函数 ,
log(x [,base])
log(x [,base])
log1p(x)
log1p(x)
log2(x)
log2(x)
log10(x)
log10(x)
Python log method | Description | Base |
---|---|---|
log(x [,base]) | It returns the natural logarithm (base-e) of the number x (if we do not define the base) or returns the given base's logarithm of the number x (if we define base). | base-e , base-any (given base) |
log1p(x) | It returns the natural logarithm (base-e) of 1 + x, where the given number is x. | base-e |
log2(x) | It returns the base-2 logarithm of the given number x. | base-2 |
log10(x) | It returns the base-10 logarithm of the given number x. | base-10 |
Python日志方法 | 描述 | 基础 |
---|---|---|
log(x [,base]) | 它返回数字x的自然对数(以e为底)(如果我们未定义底数)或返回给定的数字x的对数(如果我们定义底数)。 | base-e,base-any(给定的base) |
log1p(x) | 它返回1 + x的自然对数(以e为底),其中给定数字为x 。 | 基地 |
log2(x) | 它返回给定数字x的以2为底的对数。 | 基数2 |
log10(x) | 它返回给定数字x的以10为底的对数。 | 以10为基数 |
1)log(x [,base])方法 (1) log(x [,base]) method)
This method is used to 1) calculate natural logarithm i.e. log-e (using one parameter) or given value x, and 2) calculate any given base logarithm (using two parameters).
此方法用于1)计算自然对数,即log-e(使用一个参数)或给定值x ,以及2)计算任何给定的基本对数(使用两个参数)。
In this method, the second parameter (base) is optional, if we do not provide the second parameter, the logarithm will be calculated as natural logarithm (log-e).
在此方法中,第二个参数(基数)是可选的,如果我们不提供第二个参数,则对数将被计算为自然对数(log-e)。
Syntax:
句法:
log(x, [base])
Parameter(s): x – is a number whose logarithm to be calculated, base – is an optional parameter, it can be used to define any specific base of the logarithm.
参数: x –是要计算其对数的数字, base –是可选参数,可用于定义任何特定的对数底数。
Return value: float – it returns a float value, that is the base-e or base-any (using the second parameter) logarithm of the given number x.
返回值: float –返回一个浮点值,即给定数字x的base-e或base-any(使用第二个参数)的对数。
2)log1p(x)方法 (2) log1p(x) method)
This method is used to calculate the natural logarithm of 1 + x (base e).
此方法用于计算1 + x (以e为底)的自然对数。
Syntax:
句法:
log1p(x)
Parameter(s): x – is a number whose natural logarithm to be calculated.
参数: x –是要计算其自然对数的数字。
Return value: float – it returns a float value that is the base-e (natural) logarithm of 1 + x.
返回值: float-它返回一个浮点值,该浮点值是1 + x的底e(自然)对数。
3)log2(x)方法 (3) log2(x) method)
This method is used to calculate the base-2 logarithm of x.
此方法用于计算x的以2为底的对数。
Syntax:
句法:
log2(x)
Parameter(s): x is a number whose base-2 logarithm to be calculated.
参数: x是要计算的以2为底的对数的数字。
Return value: float – it returns a float value that is the base-2 logarithm of the given number x.
返回值: float-它返回一个浮点值,该浮点值是给定数字x的以2为底的对数。
4)log10(x)方法 (4) log10(x) method)
This method is used to calculate the base-10 logarithm of x.
此方法用于计算x的以10为底的对数。
Syntax:
句法:
log10(x)
Parameter(s): x is a number whose base-10 logarithm to be calculated.
参数: x是要计算的以10为底的对数的数字。
Return value: float – it returns a float value that is the base-10 logarithm of the given number x.
返回值: float-它返回一个浮点值,该值是给定数字x的以10为底的对数。
使用python日志函数在不同基础上找到对数的python代码 (Python code to find logarithms on different bases using python log functions)
# python code to demonstrate example of
# different math module log functions
# importing math module
import math
# number
x = 10
# natural logarithm (base-e) using log(x)
print("natural logarithm of ", x, " is = ", math.log(x))
# logarithm (base-5) using log(x, base)
print("base-5 logarithm of ", x, " is = ", math.log(x,5))
# natural logarithm (base-e) of 1+ number using log1p(x)
print("natural logarithm of 1+ ", x, " is = ", math.log(1+x))
# base-2 logarithm using log2(x)
print("base-2 logarithm of ", x, " is = ", math.log2(x))
# base-10 logarithm using log10(x)
print("base-10 logarithm of ", x, " is = ", math.log10(x))
Output
输出量
natural logarithm of 10 is = 2.302585092994046
base-5 logarithm of 10 is = 1.4306765580733933
natural logarithm of 1+ 10 is = 2.3978952727983707
base-2 logarithm of 10 is = 3.321928094887362
base-10 logarithm of 10 is = 1.0
Exceptions or Errors with log functions
日志功能的异常或错误
These methods return following expectations if we provide invalid values (except then the positive numbers),
如果我们提供的值无效(正数除外),则这些方法会返回预期值,
ValueError: math domain error – if we provide the negative value.
ValueError:数学域错误 –如果我们提供负值。
TypeError: a float is required – if we provide any value except a number (like string).
TypeError:如果我们提供除数字(如字符串)以外的任何值, 则需要浮点数 。
Python code to demonstrate example of exceptions with log functions
Python代码演示带有日志功能的异常示例
# python code to demonstrate example of
# log(x) with ValueError Exception
# importing math module
import math
# number
x = -10
print("Natural logarithm of ", x, " is = ", math.log(x))
Output
输出量
Traceback (most recent call last):
File "/home/main.py", line 10, in <module>
print("Natural logarithm of ", x, " is = ", math.log(x))
ValueError: math domain error
# python code to demonstrate example of
# log(x) with TypeError Exception
# importing math module
import math
# number
x = "10"
print("Natural logarithm of ", x, " is = ", math.log(x))
Output
输出量
Traceback (most recent call last):
File "/home/main.py", line 10, in <module>
print("Natural logarithm of ", x, " is = ", math.log(x))
TypeError: a float is required
翻译自: https://www.includehelp.com/python/log-functions-of-math-module.aspx
python数学模块函数