Math.sqrt()

Math.sqrt() 関数は、ある数の平方根を返します。すなわち、

x 0 , M a t h . s q r t ( x ) = x = the unique y 0 such that y 2 = x \forall x \geq 0, \mathtt{Math.sqrt(x)} = \sqrt{x} = \text{the unique} ; y \geq 0 ; \text{such that} ; y^2 = x

試してみましょう

構文

Math.sqrt(x)

引数

x

数値です。

返値

与えられた数値の平方根です。数値が負の場合、 NaN を返します。

解説

x の値が負の数であった場合、 Math.sqrt()NaN を返します。

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

Math.sqrt() の使用

js
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0

仕様書

Specification
ECMAScript Language Specification
# sec-math.sqrt

ブラウザーの互換性

BCD tables only load in the browser

関連情報