JavaScript Math オブジェクト

❮ 前章へ 次章へ ❯

JavaScript Math オブジェクトは、数値に対する数学的な作業を実行可能にします:


Math.PI;            // returns 3.141592653589793
Try it Yourself »

Math.round()

Math.round(x) は、x を最も近い整数に丸めた値にして返します:

Math.round(4.7);    // returns 5
Math.round(4.4);    // returns 4
Try it Yourself »

Math.pow()

Math.pow(x, y) は、x の値を y 乗して返します:

Math.pow(8, 2);      // returns 64
Try it Yourself »

Math.sqrt()

Math.sqrt(x) は、x の平方根を返します:

Math.sqrt(64);      // returns 8
Try it Yourself »

Math.abs()

Math.abs(x) は、x の絶対値(正の値)を返します:

Math.abs(-4.7);     // returns 4.7
Try it Yourself »

Math.ceil()

Math.ceil(x) は、x に最も近い整数に切り上げた値を返します:

Math.ceil(4.4);     // returns 5
Try it Yourself »

Math.floor()

Math.floor(x) は、x に最も近い整数に切り下げた値を返します:

Math.floor(4.7);    // returns 4
Try it Yourself »

Math.sin()

Math.sin(x) は、角度 x(ラジアン指定)の sine (-1 と 1 の間の値)を返します。

ラジアンの代わりに度数を使用する場合は、度数をラジアンに変換する必要があります:

ラジアン = 角度 x PI / 180.

Math.sin(90 * Math.PI / 180);     // returns 1 (the sine of 90 degrees)
Try it Yourself »

Math.cos()

Math.sin(x) は、角度 x(ラジアン指定)の cosine (-1 と 1 の間の値)を返します。

ラジアンの代わりに度数を使用する場合は、度数をラジアンに変換する必要があります:

ラジアン = 角度 x PI / 180.

Math.cos(0 * Math.PI / 180);     // returns 1 (the cos of 0 degrees)
Try it Yourself »

Math.min() and Math.max()

Math.min() と Math.max() は、引数のリスト中における最小値または最大値を探すために使用できます:

Math.min(0, 150, 30, 20, -8, -200);  // returns -200
Try it Yourself »

Math.max(0, 150, 30, 20, -8, -200);  // returns 150
Try it Yourself »

Math.random()

Math.random() は、0 以上 1 未満の間の乱数を返します:

Math.random();     // returns a random number
Try it Yourself »

このチュートリアルの次の章で Math.random() について学習します。


Math プロパティ (定数)

JavaScriptには、Math オブジェクトでアクセスできる 8 つの数学定数が用意されています:

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E
Try it Yourself »

Math コンストラクタ

他のグローバルオブジェクトとは異なり、Math オブジェクトにはコンストラクタがありません。メソッドとプロパティは static です。

すべてのメソッドとプロパティ(定数)は、Math オブジェクトを作成せずに使用できます。


Math オブジェクト・メソッド

メソッド 説明
abs(x) x の絶対値を返します
acos(x) x の cos-1 をラジアンで返します
asin(x) x の sin-1 をラジアンで返します
atan(x) x の tan-1 を、-PI / 2 と PI / 2 ラジアンの間の数値で返します
atan2(y, x) 引数の商の tan-1 を返します
ceil(x) 最も近い整数に切り上げた x の値を返します
cos(x) x の cos を返します(x はラジアンです)
exp(x) Ex の値を返します
floor(x) 最も近い整数に切り捨てた x の値を返します
log(x) x の自然対数(底 E)を返します
max(x, y, z, ..., n) 最大の値を持つ数値を返します
min(x, y, z, ..., n) 最小の値を持つ数値を返します
pow(x, y) x を y 乗した値を返します
random() 0 と 1 の間の乱数を返します
round(x) 最も近い整数に丸めた x の値を返します
sin(x) x の sin を返します(x はラジアンです)
sqrt(x) x の平方根を返します
tan(x) 角度の tan を返します

完全な Math リファレンス

完全なリファレンスは、完全な Math オブジェクトリファレンスをご覧ください。

リファレンスには、すべての Math プロパティとメソッドの説明と例が含まれています。


練習問題による自己診断

Exercise 1 »   Exercise 2 »   Exercise 3 »   Exercise 4 »


❮ 前章へ 次章へ ❯