轉(zhuǎn)載來源: python中math模塊常用的方法整理
- 導(dǎo)入函數(shù)
import math
- e
#表示一個常量
>>> math.e
2.718281828459045
- exp
#返回math.e,也就是2.71828的x次方
exp(x)
>>> math.exp(1)
2.718281828459045
>>> math.exp(2)
7.38905609893065
>>> math.exp(3)
20.085536923187668
- pi
#數(shù)字常量,圓周率
>>> print(math.pi)
3.141592653589793
- ceil
#取大于等于x的最小的整數(shù)值氨肌,如果x是一個整數(shù)逛薇,則返回x
ceil(x)
>>> math.ceil(4.01)
5
>>> math.ceil(-3.99)
-3
- floor
#取小于等于x的最大的整數(shù)值找蜜,如果x是一個整數(shù),則返回自身
floor(x)
>>> math.floor(4.999)
4
>>> math.floor(-4.01)
-5
- copysign
#把y的正負號加到x前面,可以使用0
copysign(x, y)
>>> math.copysign(2,3)
2.0
>>> math.copysign(2,-3)
-2.0
- fabs
#返回x的絕對值
fabs(x)
>>> math.fabs(-0.003)
0.003
>>> math.fabs(100)
100.0
- trunc
#返回x的整數(shù)部分
trunc(x:Real) -> Integral
>>> math.trunc(6.789)
6
>>> math.trunc(math.pi)
3
- factorial
#取x的階乘的值糟趾,x為負數(shù)或非數(shù)值則報錯
factorial(x) -> Integral
>>> math.factorial(1)
1
>>> math.factorial(2)
2
>>> math.factorial(3)
6
- gcd
#返回x和y的最大公約數(shù)
gcd(x, y) -> int
>>> math.gcd(8,6)
2
>>> math.gcd(40,20)
20
- hypot
#得到(x**2+y**2),平方的值的開根號
hypot(x, y)
>>> math.hypot(3,4)
5.0
>>> math.hypot(6,8)
10.0
- isinf
#如果x是正無窮大或負無窮大,則返回True,否則返回False
isinf(x) -> bool
>>> math.isinf(234)
False
>>> math.isinf(0.1)
False
- log
#返回x的自然對數(shù)逢唤,默認以e為基數(shù)拉讯,base參數(shù)給定時,將x的對數(shù)返回給定的base,計算式為:log(x) or log(base)
>>> math.log(10)
2.302585092994046
>>> math.log(8,2)
3.0
- log10
#返回x的以10為底的對數(shù)
log10(x)
>>> math.log10(10)
1.0
>>> math.log10(100)
2.0
- modf
#返回由x的小數(shù)部分和整數(shù)部分組成的元組
modf(x)
>>> math.modf(math.pi)
(0.14159265358979312, 3.0)
>>> math.modf(12.34)
(0.33999999999999986, 12.0)
- sqrt
#求x的平方根
sqrt(x)
>>> math.sqrt(100)
10.0
- pow
#返回x的y次方鳖藕,即x**y
pow(x, y)
>>> math.pow(3,4)
81.0
- degrees
#把x從弧度轉(zhuǎn)換成角度
degrees(x)
>>> math.degrees(math.pi/4)
45.0
>>> math.degrees(math.pi)
180.0
- radians
#把角度x轉(zhuǎn)換成弧度
radians(x)
>>> math.radians(45)
0.7853981633974483
- sin
#求x(x為弧度)的正弦值
sin(x)
>>> math.sin(math.pi/4)
0.7071067811865475
>>> math.sin(math.pi/2)
1.0
- cos
#求x的余弦魔慷,x必須是弧度
cos(x)
#math.pi/4表示弧度,轉(zhuǎn)換成角度為45度
>>> math.cos(math.pi/4)
0.7071067811865476
math.pi/3表示弧度著恩,轉(zhuǎn)換成角度為60度
>>> math.cos(math.pi/3)
0.5000000000000001
- tan
#返回x(x為弧度)的正切值
tan(x)
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257