math模块

序号

方法

功能

示例

1

math.ceil

取大于等于x的最小的整数值,如果x是一个整数,则返回x

print(math.ceil(10.1))
# 11
print(math.ceil(-3.1))
# -3

2

math.copysign

把y的正负号加到x前面,可以使用0

print(math.copysign(3,-4))
# -3.0
print(math.copysign(-1,0))
# 1.0

3

math.cos

求x的余弦,x必须是弧度

# math.pi/6表示弧度,转换成角度为30度
print(math.cos(math.pi/6))
# 0.8660254037844387
# math.pi/4表示弧度,转换成角度为45度
print(math.cos(math.pi/4))
# 0.7071067811865476
# math.pi/3表示弧度,转换成角度为60度
print(math.cos(math.pi/3))
# 0.5000000000000001

4

math.degrees

把x从弧度转换成角度

# degrees:把x从弧度转换成角度
print(math.degrees(math.pi/6))
# 29.999999999999996
print(math.degrees(math.pi/4))
# 45.0
print(math.degrees(math.pi/3))
# 59.99999999999999
print(math.degrees(math.pi))
# 180.0

5

math.e

表示一个常量

print(math.e)
# 2.718281828459045

6

math.exp

返回math.e,也就是2.71828的x次方

print(math.exp(1))
# 2.718281828459045
print(math.exp(2))
# 7.38905609893065
print(math.exp(3))
# 20.085536923187668

7

math.expm1

返回math.e的x(其值为2.71828)次方的值减1

print(math.expm1(1))
# 1.718281828459045
print(math.expm1(2))
# 6.38905609893065
print(math.expm1(3))
# 19.085536923187668

8

math.fabs

返回x的绝对值

print(math.fabs(10))
# 10.0
print(math.fabs(-10))
# 10.0

9

math.factorial

取x的阶乘的值

print(math.factorial(3))
# 6

10

math.floor

取小于等于x的最大的整数值,如果x是一个整数,则返回自身

print(math.floor(2.3))
# 2
print(math.floor(-3.3))
# -4

11

math.fmod

得到x/y的余数,其值是一个浮点数

print(math.fmod(2,3))
# 2.0
print(math.fmod(3,2.1))
# 0.8999999999999999

12

math.frexp

返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1

print(math.frexp(10))
# (0.625, 4)
print(math.frexp(20))
# (0.625, 5)
print(math.frexp(100))
# (0.78125, 7)

13

math.fsum

对迭代器里的每个元素进行求和操作

print(math.fsum([1,2,3,4,5]))
# 15.0
print(math.fsum((1,2,3,4,5)))
# 15.0

14

math.gcd

返回x和y的最大公约数

print(math.gcd(9,15))
# 3
print(math.gcd(8,12))
# 4

15

math.hypot

返回x和y的平方和的平方根,sqrt(x*x + y*y)

print(math.hypot(3,4))
# 5.0
16math.isfinite如果x不是正无穷大或负无穷大,则返回True,否则返回False
float('inf')代表正无穷,float('-inf')代表负无穷print(math.isfinite(10))
# True
print(math.isfinite(float('inf')))
# False

17

math.isinf

如果x是正无穷大或负无穷大,则返回True,否则返回False
float('inf')代表正无穷,float('-inf')代表负无穷

print(math.isinf(10))
# False
print(math.isinf((float('inf'))))
# True

18

math.isnan

如果x不是数字True,否则返回False

print(math.isnan(10))
# False

19

math.ldexp

返回x*(2**i)的值

print(math.ldexp(2,3))
# 16.0

20

math.log

log(x[, base]),返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)

print(math.log(2))
# 0.6931471805599453
print(math.log(3,10))
# 0.47712125471966244

21

math.log10

返回x的以10为底的对数

print(math.log10(2))
# 0.3010299956639812

22

math.log1p

返回x+1的自然对数(基数为e)的值

print(math.log1p(2))
# 1.0986122886681098

23

math.log2

返回x的基2对数

print(math.log2(2))
# 1.0
print(math.log2(10))
# 3.321928094887362

24

math.modf

返回由x的小数部分和整数部分组成的元组

print(math.modf(2.3456))
# (0.34560000000000013, 2.0)

25

math.pi

数字常量,圆周率

print(math.pi)
# 3.141592653589793

26

math.pow

返回x的y次方,即x**y

print(math.pow(2,3))
# 8.0

27

math.radians

把角度x转换成弧度

print(math.radians(30))
# 0.5235987755982988
print(math.radians(45))
# 0.7853981633974483
print(math.radians(60))
# 1.0471975511965976
print(math.radians(180))
# 3.141592653589793

28

math.sin

求x(x为弧度)的正弦值

print(math.sin(math.pi/6))
# 0.49999999999999994 30度
print(math.sin(math.pi/4))
# 0.7071067811865475 45度
print(math.sin(math.pi/3))
# 0.8660254037844386 60度

29

math.sqrt

求x的平方根

print(math.sqrt(9))
# 3.0

30

math.tan

返回x(x为弧度)的正切值

print(math.tan(math.pi/6))
# 0.5773502691896257
print(math.tan(math.pi/4))
# 0.9999999999999999
print(math.tan(math.pi/3))
# 1.7320508075688767

31

math.trunc

返回x的整数部分

print(math.trunc(0.12))
# 0
print(math.trunc(1.23))
# 1

原文地址:https://www.cnblogs.com/dxnui119/p/10145231.html