Math
Math
は、数値を処理するためのデータプロパティとメソッドを持つオブジェクトです。JavaScriptにモジュールが登場するずっと前に作成されたため、「貧者のモジュール」と考えることができます。
Math.E: number
[ES1]
オイラー数、自然対数の底、およそ2.7182818284590452354。
Math.LN10: number
[ES1]
10の自然対数、およそ2.302585092994046。
Math.LN2: number
[ES1]
2の自然対数、およそ0.6931471805599453。
Math.LOG10E: number
[ES1]
底を10としたeの対数、およそ0.4342944819032518。
Math.LOG2E: number
[ES1]
底を2としたeの対数、およそ1.4426950408889634。
Math.PI: number
[ES1]
数学定数π、円の円周と直径の比率、およそ3.1415926535897932。
Math.SQRT1_2: number
[ES1]
1/2の平方根、およそ0.7071067811865476。
Math.SQRT2: number
[ES1]
2の平方根、およそ1.4142135623730951。
Math.cbrt(x: number): number
[ES6]
x
の立方根を返します。
> Math.cbrt(8)2
Math.exp(x: number): number
[ES1]
ex
を返します(eはオイラー数)。Math.log()
の逆関数です。
> Math.exp(0)1
> Math.exp(1) === Math.Etrue
Math.expm1(x: number): number
[ES6]
Math.exp(x)-1
を返します。Math.log1p()
の逆関数です。非常に小さな数(0に近い分数)は、より高い精度で表現されます。そのため、この関数は.exp()
が1に近い値を返す場合、より正確な値を返します。
Math.log(x: number): number
[ES1]
x
の自然対数(底はe、オイラー数)を返します。Math.exp()
の逆関数です。
> Math.log(1)0
> Math.log(Math.E)1
> Math.log(Math.E ** 2)2
Math.log1p(x: number): number
[ES6]
Math.log(1 + x)
を返します。Math.expm1()
の逆関数です。非常に小さな数(0に近い分数)は、より高い精度で表現されます。そのため、.log()
の引数が1に近い場合、この関数により正確な引数を指定できます。
Math.log10(x: number): number
[ES6]
底を10としたx
の対数を返します。10 ** x
の逆関数です。
> Math.log10(1)0
> Math.log10(10)1
> Math.log10(100)2
Math.log2(x: number): number
[ES6]
底を2としたx
の対数を返します。2 ** x
の逆関数です。
> Math.log2(1)0
> Math.log2(2)1
> Math.log2(4)2
Math.pow(x: number, y: number): number
[ES1]
x
y
、x
のy
乗を返します。x ** y
と同じです。
> Math.pow(2, 3)8
> Math.pow(25, 0.5)5
Math.sqrt(x: number): number
[ES1]
x
の平方根を返します。x ** 2
の逆関数です。
> Math.sqrt(9)3
丸めとは、任意の数を整数(小数部のない数)に変換することです。以下の関数は、丸めに対するさまざまなアプローチを実装しています。
Math.ceil(x: number): number
[ES1]
x
≤ i
となる最小の(−∞に最も近い)整数i
を返します。
> Math.ceil(2.1)3
> Math.ceil(2.9)3
Math.floor(x: number): number
[ES1]
i
≤ x
となる最大の(+∞に最も近い)整数i
を返します。
> Math.floor(2.1)2
> Math.floor(2.9)2
Math.round(x: number): number
[ES1]
x
に最も近い整数を返します。x
の小数部が.5
の場合、.round()
は切り上げます(正の無限大に近い整数に)。
> Math.round(2.4)2
> Math.round(2.5)3
Math.trunc(x: number): number
[ES6]
x
の小数部を切り捨て、結果の整数を返します。
> Math.trunc(2.1)2
> Math.trunc(2.9)2
表 12 は、いくつかの代表的な入力に対する丸め関数の結果を示しています。
-2.9 |
-2.5 |
-2.1 |
2.1 |
2.5 |
2.9 |
|
---|---|---|---|---|---|---|
Math.floor |
-3 |
-3 |
-3 |
2 |
2 |
2 |
Math.ceil |
-2 |
-2 |
-2 |
3 |
3 |
3 |
Math.round |
-3 |
-2 |
-2 |
2 |
3 |
3 |
Math.trunc |
-2 |
-2 |
-2 |
2 |
2 |
2 |
すべての角度はラジアンで指定されます。度とラジアン間の変換には、次の2つの関数を使用します。
function degreesToRadians(degrees) {
return degrees / 180 * Math.PI;
}.equal(degreesToRadians(90), Math.PI/2);
assert
function radiansToDegrees(radians) {
return radians / Math.PI * 180;
}.equal(radiansToDegrees(Math.PI), 180); assert
Math.acos(x: number): number
[ES1]
x
のアークコサイン(逆コサイン)を返します。
> Math.acos(0)1.5707963267948966
> Math.acos(1)0
Math.acosh(x: number): number
[ES6]
x
の逆双曲線コサインを返します。
Math.asin(x: number): number
[ES1]
x
のアークサイン(逆サイン)を返します。
> Math.asin(0)0
> Math.asin(1)1.5707963267948966
Math.asinh(x: number): number
[ES6]
x
の逆双曲線サインを返します。
Math.atan(x: number): number
[ES1]
x
のアークタンジェント(逆タンジェント)を返します。
Math.atanh(x: number): number
[ES6]
x
の逆双曲線タンジェントを返します。
Math.atan2(y: number, x: number): number
[ES1]
商y/xの逆正接を返します。
Math.cos(x: number): number
[ES1]
x
のコサインを返します。
> Math.cos(0)1
> Math.cos(Math.PI)-1
Math.cosh(x: number): number
[ES6]
x
の双曲線コサインを返します。
Math.hypot(...values: number[]): number
[ES6]
values
の二乗の和の平方根を返します(ピタゴラスの定理)。
> Math.hypot(3, 4)5
Math.sin(x: number): number
[ES1]
x
のサインを返します。
> Math.sin(0)0
> Math.sin(Math.PI / 2)1
Math.sinh(x: number): number
[ES6]
x
の双曲線サインを返します。
Math.tan(x: number): number
[ES1]
x
のタンジェントを返します。
> Math.tan(0)0
> Math.tan(1)1.5574077246549023
Math.tanh(x: number): number;
[ES6]
x
の双曲線タンジェントを返します。
Math.abs(x: number): number
[ES1]
x
の絶対値を返します。
> Math.abs(3)3
> Math.abs(-3)3
> Math.abs(0)0
Math.clz32(x: number): number
[ES6]
32ビット整数x
の先頭のゼロビット数をカウントします。DSPアルゴリズムで使用されます。
> Math.clz32(0b01000000000000000000000000000000)1
> Math.clz32(0b00100000000000000000000000000000)2
> Math.clz32(2)30
> Math.clz32(1)31
Math.max(...values: number[]): number
[ES1]
values
を数値に変換し、最大の値を返します。
> Math.max(3, -5, 24)24
Math.min(...values: number[]): number
[ES1]
values
を数値に変換し、最小の値を返します。
> Math.min(3, -5, 24)-5
Math.random(): number
[ES1]
0 ≤ n
< 1となる擬似乱数n
を返します。
/** Returns a random integer i with 0 <= i < max */
function getRandomInteger(max) {
return Math.floor(Math.random() * max);
}
Math.sign(x: number): number
[ES6]
数値の符号を返します。
> Math.sign(-8)-1
> Math.sign(0)0
> Math.sign(3)1