Math.cbrt()

Math.cbrt() 関数は、引数として与えた数の立方根を返します。すなわち、

M a t h . c b r t ( x ) = x 3 = the unique y such that y 3 = x \mathtt{Math.cbrt(x)} = \sqrt[3]{x} = \text{the unique} ; y ; \text{such that} ; y^3 = x

試してみましょう

構文

Math.cbrt(x)

引数

x

数値。

返値

与えられた数値の立方根です。

解説

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

ポリフィル

すべての x 0 x \geq 0 に対して、 x 3 = x 1 / 3 \sqrt[3]{x} = x^{1/3} が存在し、次の関数でエミュレートできます:

js
if (!Math.cbrt) {
  Math.cbrt = (function (pow) {
    return function cbrt(x) {
      // ensure negative numbers remain negative:
      return x < 0 ? -pow(-x, 1 / 3) : pow(x, 1 / 3);
    };
  })(Math.pow); // localize Math.pow to increase efficiency
}

Math.cbrt() の使用

js
Math.cbrt(NaN); // NaN
Math.cbrt(-1); // -1
Math.cbrt(-0); // -0
Math.cbrt(-Infinity); // -Infinity
Math.cbrt(0); // 0
Math.cbrt(1); // 1
Math.cbrt(Infinity); // Infinity
Math.cbrt(null); // 0
Math.cbrt(2); // 1.2599210498948732

仕様書

Specification
ECMAScript Language Specification
# sec-math.cbrt

ブラウザーの互換性

BCD tables only load in the browser

関連情報