math是python的标准库吗_python标准库math用法精要

1、ceil(x)返回大于等于x的最小整数。

>>> math.ceil(3.2)

4.0

>>> math.ceil(3)

3.0

>>> math.ceil(-3.2)

-3.0

2、floor(x)

返回小于等于x的最大整数。

>>> math.floor(3.2)

3.0

>>> math.floor(-3.2)

-4.0

3、fabs(x)返回x的绝对值。

>>> math.fabs(3)

3.0

>>> math.fabs(-3)

3.0

4、factorial(x)返回x的阶乘,要求x必须为正整数。

>>> math.factorial(3)

6

>>> math.factorial(5)

120

>>> math.factorial(6)

720

>>> math.factorial(6.1)

Traceback (most recent call last):

File "", line 1, in

math.factorial(6.1)

ValueError: factorial() only accepts integral values

5、copysign(x,y)

返回一个数,其大小为x的绝对值,而符号与y相同。

>>> math.copysign(3,-1)

-3.0

>>> math.copysign(-3.5,3)

3.5

6、fmod(x, y)

返回x对于y的余数,类似于x%y。Python社区推荐:对于实数建议使用

fmod()函数,对于整数建议使用%运算符。

>>> math.fmod(3,2)

1.0

>>> math.fmod(3,-2)

1.0

>>> 3%-2 #注意fmod和%对余数符号的处理不同

-1

>>> math.fmod(3,2.1)

0.8999999999999999

>>> 3%2.1

0.8999999999999999

>>> 1e-100%2

1e-100

>>> math.fmod(1e-100,2)

1e-100

>>> math.fmod(1e-100,2e100)

1e-100

>>> 1e-100%2e100

1e-100

>>> -1e-100%2e100

2e+100

>>> math.fmod(-1e-100,2e100) #注意,对于这种情况,fmod和%的

结果不同

-1e-100

7、frexp(x)返回尾数、指数对(m,e),使得x=m*2**e。

>>> math.frexp(4.3)

(0.5375, 3)

>>> 0.5375*2**3

4.3

>>> math.frexp(4)

(0.5, 3)

>>> 0.5*2**3

4.0

>>> math.frexp(40)

(0.625, 6)

>>> 0.625*2**6

40.0

8、ldexp(m,e)

返回x=m*2**e的值,可以理解为frexp(x)的逆运算。

>>> math.frexp(50)

(0.78125, 6)

>>> math.ldexp(0.78125,6)

50.0

9、fsum(x)

返回序列x中所有元素的和,对实数操作时,可以避免精度带来的影响

,从而得到更准确的值。

>>> x = [.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]

>>> sum(x)

0.9999999999999999

>>> len(x)

10

>>> math.fsum(x)

1.0

10、modf(x)

返回x的小数部分和整数部分,并且都与x的符号相同。

>>> math.modf(3)

(0.0, 3.0)

>>> math.modf(3.5)

(0.5, 3.0)

>>> math.modf(-3.5)

(-0.5, -3.0)

11、trunc(x)返回x的整数部分。

>>> math.trunc(3.5)

3

>>> int(3.5)

3

>>> math.trunc(4.3)

4

>>> int(4.3)

4

12、exp(x)返回e**x的值。

13、expm1(x)

返回e**x-1的值,对于很小的实数可以得到更精确的结果。

>>> math.expm1(5)

147.4131591025766

>>> math.e**5-1

147.41315910257657

>>> from math import e

>>> math.exp(1e-5) - 1

1.0000050000069649e-05

>>> math.expm1(1e-5)

1.0000050000166667e-05

14、log(x[,b])

如果不提供b参数则返回x的自然对数值,提供b参数则返回x以b为底的

对数值。

>>> math.log(100)

4.605170185988092

>>> math.log(100,10)

2.0

>>> math.log(1024,2)

10.0

15、log2(x)

类似于log(x,2),但结果更准确一些,Python 3.3开始支持。

16、log10(x)

类似于log(x,10),但结果更准确一些。

17、pow(x,y)

返回x**y的值,与内置函数pow(x,y)类似。pow(x,0)和pow(1,x)总是

返回1。如果x为负值并且y不是整数,出错。

>>> pow(0,0)

1

>>> 0**0

1

>>> math.pow(0,0)

1.0

>>> pow(-3,5)

-243

>>> pow(3,5.3)

337.8645683867659

>>> pow(-3,5.3)

Traceback (most recent call last):

File "", line 1, in

pow(-3,5.3)

ValueError: negative number cannot be raised to a

fractional power

>>> math.pow(-3,5)

-243.0

>>> math.pow(3,5.3)

337.8645683867659

>>> math.pow(-3,5.3)

Traceback (most recent call last):

File "", line 1, in

math.pow(-3,5.3)

ValueError: math domain error

18、sqrt(x)

返回x的平方根。

>>> math.sqrt(9)

3.0

>>> math.sqrt(3)

1.7320508075688772

19、acos(x)、asin(x)、atan(x)返回x的反余弦、反正弦、反正切函数值,结果为弧度。

>>> math.cos(3)

-0.9899924966004454

>>> math.acos(_)

2.9999999999999996

20、atan2(y,x)

返回atan(y/x)的值,结果为-pi与pi之间的弧度值,即原点至(x,y)点

的向量与x正轴的夹角。

>>> math.atan(1)

0.7853981633974483

>>> _/math.pi #四分之一pi

0.25

>>> math.atan2(1,1)

0.7853981633974483

>>> _/math.pi

0.25

>>> math.atan2(-1,-1)

-2.356194490192345

>>> _/math.pi #负四分之三pi

-0.75

21、sin(x)、cos(x)、tan(x)

返回x的正弦函数值、余弦函数值、正切函数值,x用弧度表示。

>>> math.sin(math.pi)

1.2246467991473532e-16

>>> math.sin(0)

0.0

>>> math.sin(math.pi/2)

1.0

>>> x = [0, math.pi/4, math.pi/2, math.pi]

>>> list(map(math.sin,x))

[0.0, 0.7071067811865475, 1.0, 1.2246467991473532e-16]

22、degrees(x)、radians(x)

实现角度与弧度的互相转换。

>>> math.radians(90) #二分之一pi

1.5707963267948966

>>> _/math.pi

0.5

>>> math.degrees(math.pi)

180.0

23、acosh(x)、asinh(x)、atanh(x)、cosh(x)、sinh(x)、tanh(x)双曲线函数。

24、erf(x)、erfc(x)、gamma(x)、lgamma(x)

分别返回误差函数、剩余误差函数、伽马函数在x处的值,以及伽马函

数在x处的值的绝对值的自然对数值。

25、常数pi和e>>> math.pi3.141592653589793>>> math.e2.718281828459045

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值