Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Math.hypot()

この記事は編集レビューを必要としています。ぜひご協力ください

この翻訳は不完全です。英語から この記事を翻訳 してください。

これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

概要

引数の二乗和の平方根を返します。すなわち

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}

構文

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

引数

value1, value2, ...
数値

説明

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

引数が与えられないなら、結果は+0となります。

少なくとも引数のうち1つが数値に変換できないなら、結果はNaNになります。

ひとつの引数では、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(3, 4, 'foo'); // NaN, +'foo' => NaN
Math.hypot(3, 4, '5');   // 7.0710678118654755, +'5' => 5
Math.hypot(-3);          // 3, the same as Math.abs(-3)

ポリフィル

次の関数を使用することでエミュレートできます。:

Math.hypot = Math.hypot || function() {
  var y = 0;
  var length = arguments.length;

  for (var i = 0; i < length; i++) {
    if (arguments[i] === Infinity || arguments[i] === -Infinity) {
      return Infinity;
    }
    y += arguments[i] * arguments[i];
  }
  return Math.sqrt(y);
};

仕様

仕様 状況 コメント
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Math.hypot' in that specification.
Standard Initial definition.

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 38 27 (27) 未サポート 25 7.1
機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート 未サポート 未サポート 27.0 (27) 未サポート 未サポート iOS 8

関連情報

ドキュメントのタグと貢献者

 このページの貢献者: shide55
 最終更新者: shide55,