math-模塊

import math

print(dir(math))
# ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos',
# 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign',
# 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial',
# 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose',
# 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
# 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh',
# 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

向上取整 math.ceil()

print(math.ceil(4.1))
# 5

向下取整math.floor()

print(math.floor(4.99))
# 4

math.copysign(x, y) 將y的正負(fù)號加到x前面,可以使用0

print(math.copysign(2, -1))
print(math.copysign(-1, -1))
print(math.copysign(-1, 2))
print(math.copysign(1, 0))

# -2.0
# -1.0
# 1.0
# 1.0

math.cos(弧度)求x的余弦,x必須是弧度

print(math.cos(math.pi/4))
# 0.7071067811865476

math.degrees(弧度) 把x從弧度轉(zhuǎn)換成角度

print(math.degrees(math.pi/4))
# 45.0

math.e e表示一個常量

print(math.e)
# 2.718281828459045

math.exp()返回math.e(其值為2.71828)的x次方

print(math.exp(2))
# 7.38905609893065

expm1()返回math.e的x(其值為2.71828)次方的值減1

print(math.expm1(2))
# 6.38905609893065

fabs()返回x的絕對值

print(math.fabs(-0.6))
# 0.6

actorial()取x的階乘的值

print(math.factorial(3))
# 6

fmod()得到x/y的余數(shù),其值是一個浮點(diǎn)數(shù)

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

math.frexp()返回一個元組(m,e),其計算方式為:x分別除0.5和1,得到一個值的范圍,2e的值在這個范圍內(nèi),e取符合要求的最大整數(shù)值,然后x/(2e),得到m的值。如果x等于0,則m和e的值都為0,m的絕對值的范圍為(0.5,1)之間,不包括0.5和1

print(math.frexp(75))
# (0.5859375, 7)

math.fsum()對迭代器里的每個元素進(jìn)行求和操作

print(math.fsum((1, 2, 3, 4)))
# 10.0

math.gcd(x, y)返回x和y的最大公約數(shù)

print(math.gcd(5, 9))
# 1

math.hypot(x, y)得到(x^2 + y^2)開平方根的值

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

math.isfinite()如果x不是無窮大的數(shù)字,則返回True,否則返回False

print(math.isfinite(0.2))
# True

math.isinf()如果x是正無窮大或負(fù)無窮大,則返回True,否則返回False

print(math.isinf(235))
# False

math.isnan()如果x不是數(shù)字返回True,否則返回False

print(math.isnan(5))
# False

math.ldexp()返回x(2*i)的值

print(math.ldexp(3, 3))
# 24.0

math.log(x,a) 如果不指定a,則默認(rèn)以e為基數(shù),a參數(shù)給定時,將 x 以a為底的對數(shù)返回。

print(math.log(math.e))
# 1.0
print(math.log(32, 2))
# 5.0

math.log10(x) 返回x以10為底的對數(shù)

print(math.log10(10))
# 1.0

math.log2(x)返回x以2為底對數(shù)

print(math.log2(2))
# 1.0

math.modf()返回由x的小數(shù)部分和整數(shù)部分組成的元組

print(math.modf(math.pi))
# (0.14159265358979312, 3.0)

math.pow(x, y)返回x的y次方,即x**y

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

math.radians()把角度x轉(zhuǎn)換成弧度

print(math.radians(45))
# 0.7853981633974483

math.sin(x)求x(x為弧度)的正弦值

print(math.sin(math.pi/4))
# 0.7071067811865476

math.sqrt()求x的平方根

print(math.sqrt(100))
# 10.0

math.tan()返回x(x為弧度)的正切值

print(math.tan(math.pi/4))
# 0.9999999999999999

math.trunc()返回x的整數(shù)部分

print(math.trunc(6.156))
# 6
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容