pin_drop当前位置:知识文库 ❯ 图文
Python math三角函数详解 - sin/cos/tan与反三角函数使用指南
概述
math 模块提供了完整的三角函数库,包括正弦(sin)、余弦(cos)、正切(tan)及其反函数(asin、acos、atan、atan2),以及角度与弧度转换函数(degrees、radians)。此外还提供了双曲函数(sinh、cosh、tanh 及其反函数)。
所有三角函数的参数和返回值均使用弧度制(radian),而非角度制(degree)。弧度是数学上更自然的角量度单位,1 弧度约等于 57.296 度。一个完整的圆周为 2π 弧度(即 360 度)。三角函数在图形处理(旋转、投影)、物理模拟(波动、振动)、导航计算(方位角)、信号处理(傅里叶变换)等领域不可或缺。
语法与函数列表
代码示例
import math
# 基本三角函数
math.sin(x) # 正弦
math.cos(x) # 余弦
math.tan(x) # 正切
# 反三角函数
math.asin(x) # 反正弦,x ∈ [-1, 1]
math.acos(x) # 反余弦,x ∈ [-1, 1]
math.atan(x) # 反正切
math.atan2(y, x) # 双参数反正切(推荐)
# 角度与弧度转换
math.degrees(x) # 弧度 → 角度
math.radians(x) # 角度 → 弧度
# 双曲函数
math.sinh(x) # 双曲正弦
math.cosh(x) # 双曲余弦
math.tanh(x) # 双曲正切参数说明
所有三角函数返回 float 类型。反三角函数返回弧度值,需用 degrees() 转换为角度。
代码示例
示例1:基本三角函数与角度转换
代码示例
import math
# 角度转弧度
angle = 30
rad = math.radians(angle)
print(f"{angle}度 = {rad:.4f}弧度")
# 三角函数
print(f"sin(30°) = {math.sin(rad):.4f}")
print(f"cos(30°) = {math.cos(rad):.4f}")
print(f"tan(30°) = {math.tan(rad):.4f}")
# 特殊角度
print(f"\nsin(0°) = {math.sin(0):.4f}")
print(f"cos(0°) = {math.cos(0):.4f}")
print(f"sin(90°) = {math.sin(math.radians(90)):.4f}")
print(f"cos(90°) = {math.cos(math.radians(90)):.6f}") # 接近0
# 弧度转角度
print(f"\nπ弧度 = {math.degrees(math.pi):.1f}度")
print(f"2π弧度 = {math.degrees(2 * math.pi):.1f}度")
# 输出:
# 30度 = 0.5236弧度
# sin(30°) = 0.5000
# cos(30°) = 0.8660
# tan(30°) = 0.5774
#
# sin(0°) = 0.0000
# cos(0°) = 1.0000
# sin(90°) = 1.0000
# cos(90°) = 0.000000
#
# π弧度 = 180.0度
# 2π弧度 = 360.0度示例2:反三角函数与坐标计算
代码示例
import math
# 已知直角三角形边长,求角度
opposite = 3 # 对边
adjacent = 4 # 邻边
hypotenuse = 5 # 斜边
# 使用 asin 求角
angle_rad = math.asin(opposite / hypotenuse)
angle_deg = math.degrees(angle_rad)
print(f"asin(3/5) = {angle_deg:.1f}°")
# 使用 acos 求角
angle_rad2 = math.acos(adjacent / hypotenuse)
print(f"acos(4/5) = {math.degrees(angle_rad2):.1f}°")
# 使用 atan 求角
angle_rad3 = math.atan(opposite / adjacent)
print(f"atan(3/4) = {math.degrees(angle_rad3):.1f}°")
# atan2 计算方位角(推荐)
points = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
print("\n各点的方位角:")
for x, y in points:
angle = math.degrees(math.atan2(y, x))
print(f" ({x:2d},{y:2d}) -> {angle:7.1f}°")
# 输出:
# asin(3/5) = 36.9°
# acos(4/5) = 36.9°
# atan(3/4) = 36.9°
#
# 各点的方位角:
# ( 1, 0) -> 0.0°
# ( 1, 1) -> 45.0°
# ( 0, 1) -> 90.0°
# (-1, 1) -> 135.0°
# (-1, 0) -> 180.0°
# (-1,-1) -> -135.0°
# ( 0,-1) -> -90.0°
# ( 1,-1) -> -45.0°示例3:圆周运动与坐标旋转
代码示例
import math
# 圆周运动:计算物体在圆上的位置
radius = 10
print("圆周运动位置:")
for angle_deg in range(0, 361, 45):
rad = math.radians(angle_deg)
x = radius * math.cos(rad)
y = radius * math.sin(rad)
print(f" {angle_deg:3d}°: ({x:7.2f}, {y:7.2f})")
# 坐标旋转:将点(x, y)绕原点旋转angle度
def rotate_point(x, y, angle_deg):
"""将点(x, y)绕原点旋转指定角度"""
rad = math.radians(angle_deg)
new_x = x * math.cos(rad) - y * math.sin(rad)
new_y = x * math.sin(rad) + y * math.cos(rad)
return new_x, new_y
# 旋转点 (3, 4) 90度
point = (3, 4)
for angle in [0, 90, 180, 270]:
rotated = rotate_point(point[0], point[1], angle)
print(f"\n({point[0]},{point[1]}) 旋转{angle}° -> ({rotated[0]:.2f}, {rotated[1]:.2f})")
# 输出:
# 圆周运动位置:
# 0°: ( 10.00, 0.00)
# 45°: ( 7.07, 7.07)
# 90°: ( 0.00, 10.00)
# 135°: ( -7.07, 7.07)
# 180°: ( -10.00, 0.00)
# 225°: ( -7.07, -7.07)
# 270°: ( 0.00, -10.00)
# 315°: ( 7.07, -7.07)
# 360°: ( 10.00, 0.00)
#
# (3,4) 旋转0° -> (3.00, 4.00)
# (3,4) 旋转90° -> (-4.00, 3.00)
# (3,4) 旋转180° -> (-3.00, -4.00)
# (3,4) 旋转270° -> (4.00, -3.00)实际应用场景
-
图形处理:2D/3D 坐标旋转、缩放、投影变换
-
物理模拟:抛体运动、简谐振动、波的传播
-
导航计算:方位角计算、两点间距离与方向
-
信号处理:傅里叶变换、频谱分析、波形生成
-
游戏开发:角色朝向、弹道计算、碰撞检测
-
机器人学:运动学正逆解、路径规划
注意事项
注意1:所有三角函数使用弧度制,不是角度制。务必使用
radians()将角度转换为弧度后再传入函数。
注意2:
tan(π/2)在数学上无定义,但 Python 会返回极大值(约 1.633e16)而非报错,这是因为浮点数无法精确表示 π/2。
注意3:
asin()和acos()的参数必须在 [-1, 1] 范围内,否则抛出ValueError。由于浮点精度,asin(1.0000001)也会报错。
注意4:优先使用
atan2(y, x)而非atan(y/x),前者能正确处理所有象限的角度,且 x=0 时不会除零。
注意5:
cos(90°)的结果不是精确的 0,而是一个极小的浮点数(约 6.12e-17),这是浮点精度导致的。
提示:如果需要高精度的三角函数运算,可以考虑使用
mpmath第三方库,它支持任意精度。
相关方法对比
小贴士
在需要大量三角函数运算的场景中(如信号处理、图形渲染),建议使用 numpy 库替代 math,因为 numpy 支持向量化运算,可对整个数组一次性计算,性能提升可达数十倍。
小结
-
math三角函数使用弧度制,需用radians()转换角度 -
sin、cos、tan计算三角函数值 -
asin、acos、atan计算反三角函数值 -
atan2(y, x)是计算方位角的最佳选择,能正确处理所有象限 -
degrees()和radians()用于角度与弧度的互转 -
浮点精度会导致
cos(90°)不精确等于 0 等问题
练习题
练习1
使用三角函数计算等边三角形(边长为 10)的面积。提示:面积 = (1/2) * a * b * sin(C)。
练习2
使用 atan2() 计算从原点到点 (3, 4) 的方位角,并转换为角度。
练习3
编写一个函数 rotate_point(x, y, angle),将点 (x, y) 绕原点旋转指定角度(度数),返回新坐标。
练习4
使用 sin 和 cos 生成一个正弦波数据表,从 0 到 2π 取 12 个等间距点,输出每个点的 x 值和 sin(x) 值。
常见问题
Python 的 math.sin() 为什么使用弧度而不是角度?
弧度是数学上更自然的角量度方式。在微积分中,三角函数的导数公式(如 d/dx(sin x) = cos x)仅在弧度制下成立。使用弧度制可以避免额外的换算系数,使数学公式更加简洁。因此包括 Python 在内的大多数编程语言都采用弧度制。
atan2(y, x) 和 atan(y/x) 有什么区别?
atan(y/x) 只能返回 [-π/2, π/2] 范围的角度,无法区分不同象限的点(如 (1,1) 和 (-1,-1) 的 y/x 相同)。而 atan2(y, x) 根据 x 和 y 的符号判断象限,返回 [-π, π] 范围的完整角度。此外,当 x=0 时 atan(y/x) 会除零报错,而 atan2 可以正确处理。
为什么 cos(90°) 的结果不是精确的 0?
这是因为浮点数无法精确表示 π/2。math.radians(90) 返回的是 π/2 的浮点近似值,因此 cos(radians(90)) 返回的是一个极小的浮点数(约 6.12e-17),而非数学上的精确 0。这是所有浮点运算中的常见现象,在比较时应使用 math.isclose() 而非直接相等判断。
math 模块支持复数三角函数吗?
math 模块不支持复数运算。如果需要对复数进行三角函数运算,请使用 cmath 模块,它提供了复数版本的 sin、cos、tan 等函数。例如 cmath.sin(1+2j) 可以正确计算复数的正弦值。
如何在 Python 中计算 tan(90°)?
tan(90°) 在数学上无定义(趋向无穷大)。在 Python 中,math.tan(math.radians(90)) 会返回一个极大的浮点数(约 1.633e16),因为浮点数无法精确表示 π/2。如果需要判断是否为无穷大,可使用 math.isinf() 函数。在实际编程中,应避免直接计算 tan(90° + k*180°) 这类值。
本文涉及AI创作
内容由AI创作,请仔细甄别