Python【math数学函数】
数论与表示函数
1.ceil()和floor()
向上向下取整
import math
print(math.ceil(3.5))
print(math.ceil(3.0))
print(math.ceil(3))
print(math.floor(4))
print(math.floor(3.5))
print(math.floor(3))
输出:
4
3
3
4
3
3
2.comb()
import math
print(math.comb(5,2))
print(math.comb(5,5))
print(math.comb(5,6))
输出:
10
1
0
3.copysign()
import math
print(math.copysign(-5,2))
print(math.copysign(5,-5))
输出:
5.0
-5.0
4.fabs()
import math
print(math.fabs(-3.5))
print(math.fabs(3))
输出:
3.5
3.0
5.factorial()
import math
print(math.factorial(0))
print(math.factorial(4))
print(math.factorial(10))
输出:
1
24
3628800
6.gcd()
import math
print(math.gcd(10,5))
print(math.gcd(11,10))
print(math.gcd(15,10))
输出:
5
1
5
7.lcm()
import math
print(math.lcm(10,5))
print(math.lcm(11,10))
print(math.lcm(15,10))
输出:
10
110
30
幂函数与对数函数
1.exp()和math.e和pow()
import math
print(math.exp(1))
print(math.e ** 1)
print(pow(math.e,1))
print('------------------')
print(math.exp(2))
print(math.e ** 2)
print(pow(math.e,2))
print('------------------')
print(math.exp(10))
print(math.e ** 10)
print(pow(math.e,10))
输出:
2.718281828459045
2.718281828459045
2.718281828459045
------------------
7.38905609893065
7.3890560989306495
7.3890560989306495
------------------
22026.465794806718
22026.465794806703
22026.465794806703
2.log()和log2()和log10()
import math
print(math.log(math.e ** 2))
print(math.log2(1024))
print(math.log10(1000))
输出:
2.0
10.0
3.0
3.sqrt(x)
import math
print(math.sqrt(10))
print(math.sqrt(16))
输出:
3.1622776601683795
4.0
三角函数
1.asin、acos()、atan()
import math
print(math.asin(1))
print(math.acos(-1))
print(math.atan(1))
2.sin()、cos()、tan()
import math
print(math.sin(math.pi / 2))
print(math.cos(math.pi))
print(math.tan(math.pi / 4))
输出:
1.0
-1.0
0.9999999999999999
常量
import math
print(math.pi)
print(math.e)
print(math.tau)
print(math.inf)
print(math.nan)
输出:
3.141592653589793
2.718281828459045
6.283185307179586
inf
nan
|