math.hypot(x, y) 返回 sqrt(x*x + y*y),即直角三角形兩直角邊為 x、y 時的斜邊長度,亦即向量 (x, y) 的 L2 範數。Python 3.8+ 支援多維:hypot(*coordinates)。
math.hypot(x, y)
>> import math
>>> math.hypot(3, 2)
3.605551275463989
>>> math.hypot(-3, 3)
4.242640687119285
>>> math.hypot(0, 2)
2.0
dist = hypot(x2-x1, y2-y1)。>> math.hypot(1, 2, 2) # 3D 向量模長
3.0
>>> math.hypot(*[3,4,12]) # 13.0