天天看點

python math_Python之math子產品

math子產品

math子產品可以幫助我們在代碼中很友善的進行資料計算,常用方法如下

ceil

取大于等于x的最小的整數值,如果x為一個整數,那麼就是x本身

ceil(x)

Return the ceiling of x as an int

Thisis the smallest intergral value >=ximportmathprint(math.ceil(59.88)) #60

floor

取小于等于x的最大的整數值,如果x是一個整數,傳回自身

floor(x)

Return the floor of x as an int

Thisis the largest integral value <=xprint(math.floor(59.55)) #59

copysign

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

print(math.copysign(1,3)) #1.0

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

cos

求x的餘弦,x必須是弧度

print(math.cos(math.pi / 4)) #math.pi表示弧度,轉換成角度為45度

print(math.cos(math.pi / 3)) #轉換成角度為60度

print(math.cos(math.pi / 6)) #轉換成角度為30度

degrees

把x從弧度轉換成角度

print(math.degrees(math.pi / 4))print(math.degrees(math.pi / 6))print(math.degrees(math.pi / 3))

e

表示一個常量

print(math.e)

exp

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

print(math.exp(1)) #傳回math.e的1次方

print(math.exp(2)) #傳回math.e的2次方

print(math.exp(3)) #傳回math.e的3次方

expm1

傳回math.e的x次方的值減1

print(math.expm1(1))print(math.expm1(2))print(math.expm1(3))

fbs

傳回x的絕對值

print(math.fbs(-0.001)) #0.001

print(math.fbs(-9.1)) #9.1

print(math.fbs(444)) #444

factorial

取x的階乘的值

print(math.factorial(1)) #1

print(math.factorial(2)) #2

print(math.factorial(3)) #6

print(math.factorial(4)) #24

fmod

得到x/y的餘數,其值是一個浮點數

print(math.fmod(10,3)) #1

print(math.fmod(20,3)) #2

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

Return the mantissa andexponent of x, as pair (m, e).

mis a float and e is an int, such that x = m * 2.**e.

If xis 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.print math.frexp(10)

fsum

對疊代器裡的每個元素進行求和操作

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

gcd

傳回x和y的最大公約數

print( math.gcd(8,6))

hypot

得到(x**2 + y**2),平方的值

print( math.hypot(3,4)) #5

print(math.hypot(6.8)) #10

isfinite

如果x是不是無窮大的數字,則傳回True,否則傳回False

print(math.isfinite(100))

isinf

如果x是正無窮大或負無窮大,則傳回True,否則傳回False

print(math.isinf(234)) #False

isnan

如果x不是數字True,否則傳回False

print(math.isnan(23))

idexp

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

print(math.ldexp(5,5))

log

傳回x的自然對數,預設以e為基數,base參數給定時,将x的對數傳回給定的base,計算式為:log(x)/log(base)

>>> math.log(10)2.302585092994046

>>> math.log(11)2.3978952727983707

>>> math.log(20)2.995732273553991

log10

傳回x的以10為底的對數

>>> math.log10(10)1.0

>>> math.log10(100)2.0

#即10的1.3次方的結果為20

>>> math.log10(20)1.3010299956639813

log1p

傳回x+1的自然對數(基數為e)的值

>>> math.log(10)2.302585092994046

>>> math.log1p(10)2.3978952727983707

>>> math.log(11)2.3978952727983707

log2

傳回x的基2對數

>>> math.log2(32)5.0

>>> math.log2(20)4.321928094887363

>>> math.log2(16)4.0

modf

傳回由x的小數部分和整數部分組成的元組

>>>math.modf(math.pi)

(0.14159265358979312, 3.0)>>> math.modf(12.34)

(0.33999999999999986, 12.0)

pi

數字常量,圓周率

>>> print(math.pi)3.141592653589793

pow

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

>>> math.pow(3,4)81.0

>>>

>>> math.pow(2,7)128.0

radians

把角度x轉換成弧度

>>> math.radians(45)0.7853981633974483

>>> math.radians(60)1.0471975511965976

sin

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

>>> math.sin(math.pi/4)0.7071067811865475

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

>>> math.sin(math.pi/3)0.8660254037844386

sqrt

求x的平方根

>>> math.sqrt(100)10.0

>>> math.sqrt(16)4.0

>>> math.sqrt(20)4.47213595499958

tan

傳回x(x為弧度)的正切值

>>> math.tan(math.pi/4)0.9999999999999999

>>> math.tan(math.pi/6)0.5773502691896257

>>> math.tan(math.pi/3)1.7320508075688767

trunc

傳回x的整數部分

>>> math.trunc(6.789)6

>>>math.trunc(math.pi)3

>>> math.trunc(2.567)2