实现说明
演示用的数据放在这几个模块级绑定中:p1、p2。
使用要点
- 跑完文件,输出里会留下标记——
hypot 2d:、hypot 3d:、dist 2d:。
常见问题
有第三方依赖吗?
没有。依赖清单是空的,代码只用标准库。 状态都挂在模块顶层:p1、p2。
代码示例
import math
# hypot computes sqrt(x*x + y*y + ...) without overflow for big components
print("hypot 2d:", math.hypot(3, 4))
print("hypot 3d:", math.hypot(1, 2, 2))
# dist measures the straight-line distance between two points
p1 = (2, 3)
p2 = (5, 7)
print("dist 2d:", math.dist(p1, p2))
# dist also accepts higher-dimensional points of equal length
print("dist 3d:", math.dist((0, 0, 0), (1, 2, 2)))
# dist(a, b) equals hypot of the component differences
print("match:", math.dist(p1, p2) == math.hypot(5 - 2, 7 - 3))