Python math.hypot()メソッド
例
垂線と底辺がわかっている直角三角形の斜辺を見つけます。
#Import math Library
import math
#set perpendicular and base
parendicular = 10
base = 5
#print the hypotenuse of a right-angled
triangle
print(math.hypot(parendicular, base))
定義と使用法
このmath.hypot()
メソッドは、ユークリッドノルムを返します。ユークリッドノルムは、原点から指定された座標までの距離です。
Python 3.8より前では、このメソッドは直角三角形の斜辺を見つけるためにのみ使用されていました:sqrt(x * x + y * y)。
Python 3.8以降、このメソッドはユークリッドノルムの計算にも使用されます。n次元の場合、渡される座標は(x1、x2、x3、...、xn)のように想定されます。したがって、原点からのユークリッド距離はsqrt(x1 * x1 + x2 * x2 + x3 * x3 .... xn * xn)によって計算されます。
構文
math.hypot(x1, x2, x3, ..., xn)
パラメータ値
Parameter | Description |
---|---|
x1, x2, x3, ..., xn | Required. Two or more points representing coordinates |
技術的な詳細
戻り値: | n個float の入力の場合は原点からのユークリッド距離を表す値、または2個の入力の場合は直角三角形の斜辺を表す値 |
---|---|
変更ログ: | 3.8以降:n次元の点もサポートします。以前のバージョンは2次元ポイントのみをサポートします |
その他の例
例
与えられた点のユークリッドノルムを見つけます。
#Import math Library
import math
#print the Euclidean norm for
the given points
print(math.hypot(10, 2, 4, 13))
print(math.hypot(4, 7, 8))
print(math.hypot(12, 14))