简介
Python标准库提供的math模块实现了对C标准定义的数学函数的访问。
注意,math模块的函数不适用于复数,对于复数,使用cmath模块中的函数进行处理。
常量
math模块中包含一些数学中的常量值,如:
math.pi
#数学常数 π = 3.141592...,精确到可用精度
math.e
#数学常数 e = 2.718281...,精确到可用精度
math.tau
#数学常数 τ = 6.283185...,精确到可用精度。Tau 是一个圆周常数,等于 2π,圆的周长与半径之比。
math.inf
#浮点正无穷大。(对于负无穷大,使用 -math.inf)
math.nan
#浮点“非数字”值(NaN)
其他
查看math模块中包含的内容:
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>
在math模块中,都是数学相关的方法,没有特别的类定义。具体使用时可参考官方文档。