文章目录
1、引用math标准库
- 第一种方式:import math
对math标准库中函数采用math.()形式使用
import math
math.ceil(0,2)
11
- 第二种方式:from math import <函数名>
对math标准库中函数可以直接采用<函数名>()形式使用
from math import floor
floor(10.2)
10
2、math 标准库解析
以下展示全部采用第一种引用(import math)
2.1 math标准库包括4个数学常数
常数 | 数学表示 | 描述 |
---|---|---|
math.pi | π | 圆周率,值为3.141592653589793 |
math.e | e | 自然对数,值为2.718281828459045 |
math.inf | ∞ | 正无穷大,负无穷大为-math.inf |
math.nan | 非浮点数标记,NAN(Not a Number) |
例如
e
#表示一个常量
>>> math.e
2.718281828459045
pi
#数字常量,圆周率
>>> print(math.pi)
3.141592653589793
2.2math标准库包括16个数值表示函数
例如
ceil(x, /)
Return the ceiling of x as an Integral.
将X的上限作为积分返回。
This is the smallest integer >= x.
这是最小的整数>=x。
#取大于等于x的最小的整数值,如果x是一个整数,则返回x
>>> math.ceil(1.01)
2
>>> math.ceil(1.99)
2
>>> math.ceil(-2.99)
-2
>>> math.ceil(-2.01)
-2
fabs(x, /)
Return the absolute value of the float x.
返回浮点X的绝对值。
>>> math.fabs(-0.001)
0.001
>>> math.fabs(-100)
100.0
fmod(x, y, /)
Return fmod(x, y), according to platform C.
返回FMOD(X,Y),根据平台C。
#得到x/y的余数,其值是一个浮点数
>>> math.fmod(25,4)
1.0
>>> math.fmod(24,7)
3.0
isinf(x, /)
Return True if x is a positive or negative infinity, and False otherwise.
如果x是正的或负的无穷大,则返回真,否则为假。
#如果x是正无穷大或负无穷大,则返回True,否则返回False
>>> math.isinf(10000)
False
>>> math.isinf(0.00001)
False
2.3math标准库包括8个幂对数函数
例如
pow(x, y, /)
Return xy (x to the power of y).
返回xy(x到y的幂)。
>>> math.pow(2,5)
32.0
exp(x, /)
Return e raised to the power of x.
返回E提高到X的功率。
>>> math.exp(2)
7.38905609893065
sqrt(x, /)
Return the square root of x.
返回X的平方根。
>>> math.sqrt(25)
5.0
>>> math.sqrt(400)
20.0
log10(x, /)
Return the base 10 logarithm of x.
返回x的基10对数。
>>>math.log10(1000)
3.0
>>> math.log10(100)
2.0
log1p(x, /)
Return the natural logarithm of 1+x (base e).
返回1±x(基E)的自然对数。
The result is computed in a way which is accurate for x near zero.
结果以精确接近x的方式计算。
>>> math.log1p(10)
2.3978952727983707
log2(x, /)
Return the base 2 logarithm of x.
返回x的基2对数。
>>> math.log2(32)
5.0
>>> math.log2(20)
4.321928094887363
2.4math标准库包括16个三角双曲函数
degrees(x, /)
Convert angle x from radians to degrees.
将角度X从弧度转换成度。
>>> math.degrees(math.pi/4)
45.0
radians(x, /)
Convert angle x from degrees to radians.
将角度x从度转换为弧度。
>>>math.radians(30)
π/6
hypot(x, y)
#得到(x2+y2),平方的值
hypot(x, y)
Return the Euclidean distance, sqrt(xx + yy).
>>>math.hypot(3,4)
5.0
求x的正弦,x必须是弧度
sin(x, /)
Return the sine of x (measured in radians).
返回X的正弦(以弧度测量)。
math.pi/6表示弧度,转换成角度为30度
>>> math.sin(math.pi/6)
0.5000000000000001
cos(x, /)
Return the cosine of x (measured in radians).
返回X的余弦(用弧度测量)。
#math.pi/4表示弧度,转换成角度为45度
>>> math.cos(math.pi/4)
0.7071067811865476
tan(x, /)
Return the tangent of x (measured in radians).
返回X的切线(以弧度测量)。
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257
2.5math标准库包括4个高等特殊函数
例如
erf(x, /)
Error function at x.
x的误差函数。
erfc(x, /)
Complementary error function at x.
x的互补误差函数
以上只列举了几个常用函数的例子,其他请参考链接:math- 数学函数