Python math.hypot() 方法及代码示例

math.hypot(*coordinates):返回欧几里得范数,sqrt(sum(x**2 for x in coordinates))

这是从原点到坐标给定点的向量长度

对于一个二维点 (x, y),这等价于使用毕达哥拉斯定义 sqrt(x*x + y*y) 计算一个直角三角形的斜边

定义

math.hypot(*coordinates)

返回欧几里得范数,sqrt(sum(x**2 for x in coordinates))

参数

参数类型参数名称参数描述
int,floatx数值x

返回值

返回欧几里得范数,sqrt(sum(x**2 for x in coordinates))

示例

import math

print("math.hypot(-1, 1, 1, 4) : ", math.hypot(-1, 1, 1, 4))
print("math.hypot(5, 8, -9, 1, 0) : ", math.hypot(5, 8, -9, 1, 0))
print("math.hypot(-10) : ", math.hypot(-10))
print("math.hypot(0.5, -0.5) : ", math.hypot(0.5, -0.5))

执行结果为 :

math.hypot(-1, 1, 1, 4) :  4.358898943540674
math.hypot(5, 8, -9, 1, 0) :  13.076696830622021
math.hypot(-10) :  10.0
math.hypot(0.5, -0.5) :  0.7071067811865476