欢迎访问宙启技术站
智能推送

Python的数学函数有哪些?详解math库中10个常见函数

发布时间:2023-06-26 10:13:46

Python数学函数是在数学计算中所使用的函数,用于执行各种数学运算,如三角函数、指数函数、对数函数、幂函数、统计函数等等。Python中内置了许多标准数学函数,同时还有许多扩展的数学函数可以通过导入math库来使用。

math库是Python中内置的数学计算库,提供了许多数值计算和几何计算函数。下面详解math库中10个常见的数学函数。

1. math.sqrt(x)

- 作用:返回x的平方根。

- 参数:x(数值型)

- 示例代码:

import math

a = math.sqrt(9)
print(a)   # 3.0

2. math.exp(x)

- 作用:返回x的指数函数值,即e的x次幂。

- 参数:x(数值型)

- 示例代码:

import math

a = math.exp(2)
print(a)  # 7.3890560989306495

3. math.log(x, base)

- 作用:返回x的对数,指定对数的基数为base(默认情况下为e)。

- 参数:x(数值型),base(数值型)

- 示例代码:

import math

a = math.log(10)
print(a)   # 2.302585092994046

b = math.log(16, 2)  # 求2的log16
print(b)  # 4.0

4. math.sin(x)

- 作用:返回x(弧度)的正弦值。

- 参数:x(数值型)

- 示例代码:

import math

a = math.sin(math.pi/2)
print(a)   # 1.0

5. math.cos(x)

- 作用:返回x(弧度)的余弦值。

- 参数:x(数值型)

- 示例代码:

import math

a = math.cos(math.pi)
print(a)   # -1.0

6. math.tan(x)

- 作用:返回x(弧度)的正切值。

- 参数:x(数值型)

- 示例代码:

import math

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

7. math.atan(x)

- 作用:返回x(弧度)的反正切值。

- 参数:x(数值型)

- 示例代码:

import math

a = math.atan(1)
print(a)   # 0.7853981633974483

8. math.degrees(x)

- 作用:将x(弧度)转换为角度。

- 参数:x(数值型)

- 示例代码:

import math

a = math.degrees(math.pi/2)
print(a)   # 90.0

9. math.radians(x)

- 作用:将x(角度)转换为弧度。

- 参数:x(数值型)

- 示例代码:

import math

a = math.radians(90)
print(a)   # 1.5707963267948966

10. math.pi

- 作用:返回圆周率π的值。

- 示例代码:

import math

a = math.pi
print(a)   # 3.141592653589793

以上就是math库中十个常见的数学函数的介绍及示例代码。使用这些数学函数可以方便地进行各种数学计算,提高Python的数值计算能力。