python中math的用法
时间: 2023-09-16 15:13:02 浏览: 44
Python中的math模块提供了许多数学函数和常数。以下是一些常用的math函数:
1. math.ceil(x):返回大于或等于x的最小整数。
2. math.floor(x):返回小于或等于x的最大整数。
3. math.sqrt(x):返回x的平方根。
4. math.pow(x, y):返回x的y次方。
5. math.log(x[, base]):返回x的自然对数(以e为底),或 base 的对数。
6. math.sin(x):返回x的正弦值。
7. math.cos(x):返回x的余弦值。
8. math.tan(x):返回x的正切值。
9. math.degrees(x):将弧度转换为角度。
10. math.radians(x):将角度转换为弧度。
以下是一些常用的math常数:
1. math.pi:圆周率。
2. math.e:自然常数e。
例如:
```python
import math
x = 3.14
y = 2
print(math.ceil(x)) # 输出 4
print(math.floor(x)) # 输出 3
print(math.sqrt(x)) # 输出 1.77200451467
print(math.pow(x, y)) # 输出 9.8596
print(math.log(x)) # 输出 1.14422279992
print(math.sin(x)) # 输出 0.00159265291649
print(math.cos(x)) # 输出 -0.999998731727
print(math.tan(x)) # 输出 -0.00159265493641
print(math.degrees(x)) # 输出 179.908747671
print(math.radians(180)) # 输出 3.14159265359
print(math.pi) # 输出 3.141592653589793
print(math.e) # 输出 2.718281828459045
```