From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/math/hypot/index.html | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/math/hypot/index.html (limited to 'files/ja/web/javascript/reference/global_objects/math/hypot/index.html') diff --git a/files/ja/web/javascript/reference/global_objects/math/hypot/index.html b/files/ja/web/javascript/reference/global_objects/math/hypot/index.html new file mode 100644 index 0000000000..83f8161db1 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/math/hypot/index.html @@ -0,0 +1,127 @@ +--- +title: Math.hypot() +slug: Web/JavaScript/Reference/Global_Objects/Math/hypot +tags: + - JavaScript + - Math + - Method + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Math/hypot +--- +
{{JSRef}}
+ +

Math.hypot() 関数は、各引数の二乗の合計値の平方根を返します。

+ +

Math.hypot(v1,v2,,vn)=i=1nvi2=v12+v22++vn2\mathtt{\operatorname{Math.hypot}(v_1, v_2, \dots, v_n)} = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

+ +
{{EmbedInteractiveExample("pages/js/math-hypot.html")}}
+ + + +

構文

+ +
Math.hypot([value1[, value2[, ...]]])
+ +

引数

+ +
+
value1, value2, ...
+
数値です。
+
+ +

返値

+ +

与えられた各引数の二乗の合計値の平方根を返します。1つ上の引数が数値に変換できなかった場合は、 {{jsxref("NaN")}} が返されます。

+ +

解説

+ +

直角三角形の斜辺や、複素数の大きさを計算するには Math.sqrt(v1*v1 + v2*v2) という公式を用い、ここで v1 と v2 は三角形の辺の長さであったり、複素数の実数と複素数部分であったりします。二次元またはそれ以上の次元における対応する距離は、 Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4) のように平方根の下にさらに多くの平方を足すことで計算できます。

+ +

この関数はこの計算をより簡単に、より高速に行います。 Math.hypot(v1, v2) または Math.hypot(v1, v2, v3, v4, ...) を呼び出すだけです。

+ +

Math.hypot はまた、数値が非常に大きい場合のオーバーフロー/アンダーフローの問題を回避します。 JS で表現できる最大の数は Number.MAX_VALUE で、これは約 10308 です。数字の大きさが約 10154 よりも大きい場合、その2乗を取ると無限大になります。例えば、 Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity. 代わりに hypot() を使うと、 Math.hypot(1e200, 1e200) = 1.4142...e+200 となり、より良い答えが得られます。これは非常に小さな数の場合にも当てはまります。 Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0 ですが、 Math.hypot(1e-200, 1e-200) = 1.4142...e-200 となります。

+ +

hypot()Math の静的メソッドなので、常に Math.hypot() として使用し、自分で Math オブジェクトを生成してそのメソッドとして使用しないでください。 (Math にはコンストラクターがありません)。

+ +

引数が与えられなかった場合、結果は +0 になります。引数のいずれかが ±Infinity であった場合、結果は Infinity になります。引数のいずれかが NaN であった場合は (他に ±Infinity の引数がない限り)、結果は NaN になります。数値に変換できない引数があった場合は、結果は {{jsxref("NaN")}} になります。

+ +

引数が1つの場合、 Math.hypot()Math.abs() と同等です。

+ +

+ +

Math.hypot() の使用

+ +
Math.hypot(3, 4);          // 5
+Math.hypot(3, 4, 5);       // 7.0710678118654755
+Math.hypot();              // 0
+Math.hypot(NaN);           // NaN
+Math.hypot(NaN, Infinity); // Infinity
+Math.hypot(3, 4, 'foo');   // NaN, since +'foo' => NaN
+Math.hypot(3, 4, '5');     // 7.0710678118654755, +'5' => 5
+Math.hypot(-3);            // 3, the same as Math.abs(-3)
+
+ +

ポリフィル

+ +

オーバーフロー/アンダーフローの問題を処理しないナイーブなアプローチです。

+ +
if (!Math.hypot) Math.hypot = function() {
+  var y = 0, i = arguments.length, containsInfinity = false;
+  while (i--) {
+    var arg = arguments[i];
+    if (arg === Infinity || arg === -Infinity)
+      containsInfinity = true
+    y += arg * arg
+  }
+  return containsInfinity ? Infinity : Math.sqrt(y)
+}
+
+ +

アンダーフローやオーバーフローを回避するポリフィルです。

+ +
if (!Math.hypot) Math.hypot = function () {
+  var max = 0;
+  var s = 0;
+  var containsInfinity = false;
+  for (var i = 0; i < arguments.length; ++i) {
+    var arg = Math.abs(Number(arguments[i]));
+    if (arg === Infinity)
+      containsInfinity = true
+    if (arg > max) {
+      s *= (max / arg) * (max / arg);
+      max = arg;
+    }
+    s += arg === 0 && max === 0 ? 0 : (arg / max) * (arg / max);
+  }
+  return containsInfinity ? Infinity : (max === 1 / 0 ? 1 / 0 : max * Math.sqrt(s));
+};
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-math.hypot', 'Math.hypot')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.Math.hypot")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf