0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mathオブジェクト一覧

Posted at

Mathオブジェクトの概要

Math は名前空間オブジェクトで、数学的な定数と関数を提供する静的プロパティおよびメソッドがあります。
他のグローバルオブジェクトと異なり、Math はコンストラクターではありません。 Math のプロパティとメソッドはすべて静的です。
(Math - JavaScript | MDN から要約)

つまり、Math オブジェクトはコンストラクターではないので、

const mathObj = new Math();

のようなプログラムを実行すると

> TypeError: Math is not a constructor

のようなエラーが発生します。Mathオブジェクトは最初から用意されているMathそのものを使用することが絶対です。

プロパティ・メソッド一覧

Mathオブジェクトのプロパティ・メソッドの一覧です。
ジャンルごとに分けています。
:::info
文字数が多くなるのを防ぐために、Math.は省略しています。
:::

静的プロパティ

定数

プロパティ 説明 およその数値
E ネイピア数 $e$ 2.718281828459045
PI 円周率 $\pi$ 3.141592653589793

対数

プロパティ 説明 およその数値
LN10 10の自然対数 $\ln10$ 2.302585092994046
LN2 2の自然対数 $\ln2$ 0.6931471805599453
LOG10E ネイピア数の常用対数 $\log e$ 0.4342944819032518
LOG2E 2を底としたネイピア数の対数 $\log_2e$ 1.4426950408889634

平方根

プロパティ 説明 およその数値
SQRT1_2 1/2の平方根 $\sqrt{\frac{1}{2}}$ 0.7071067811865476
SQRT2 2の平方根 $\sqrt{2}$ 1.4142135623730951

静的メソッド

「プロパティ」部分のxyなどは、引数とその数、型を示すのと、「説明」での式で使用するために入れています。

「例外」では特定の数値による特別な動きを示していますが、「数値以外 → NaN」はほとんどのメソッドに当てはまるので省略します

値を操作する

メソッド 説明,式 例外
abs(x) 入力値の絶対値 $|x|$ Math.abs(-3) = 3
sqrt(x) 入力値の平方根 $\sqrt{x}$ Math.sqrt(16) = 4 負の数 → NaN
cbrt(x) 入力値の立方根 $\sqrt[3]{x}$ Math.cbrt(27) = 3
ceil(x) 入力値の切り上げ $\lceil x \rceil$ Math.ceil(3.001) = 4
floor(x) 入力値の切り捨て $\lfloor x \rfloor$ Math.floor(10.999) = 10
round(x) 入力値を四捨五入したもの $[x]$ Math.round(3.141) = 3
trunc(x) 入力値の小数部を取り除く Math.trunc(-3.14)= -3
f16round(x) 入力値に最も近い16ビット半精度浮動小数点表現1 Math.f16round(1.1) = 1.0996093752 65504を超える → 65504またはInfinity
fround(x) 入力値に最も近い32ビット単精度浮動小数点表現 Math.fround(1.05) = 1.04999995231628423 最大値(およそ $3.0428$ $\times$ $10^{38}$)を超える → Infinity

演算

メソッド 説明,式 例外
log(x) 入力値の自然対数 $\ln$ $x$ Math.log(10) = 2.302585092994046 0Infinity,
-0-Infinity,
負の数 → NaN
log1p(x) 入力値に1を足した数の自然対数 $\ln$ $(x+1)$ Math.log1p(1) = 0.6931471805599453 -0-0,
-1Infinity,
-1未満 → NaN
log10(x) 入力値の常用対数 $\log$ $x$ Math.log10(1000) = 3 log()と同じ
log2(x) 2を底とした入力値の対数 $\log_2$ $x$ Math.log2(16384) = 14 log()と同じ
exp(x) ネイピア数の累乗 $e^x$ Math.exp(2) = 7.38905609893065
expm1(x) ネイピア数の累乗から1を引いた値 $e^x-1$ Math.expm1(2) = 6.38905609893065

引数が複数あるもの

メソッド 説明,式 例外
hypot(...x) 各入力値の二乗の合計の平方根 $\sqrt{x_0^2+x_1^2+\cdots}$ Math.hypot(3, 4) = 5 引数のいずれかがInfinityまたは-InfinityInfinity
imul(x, y) 入力値の32ビット乗算 $xy$4 Math. = 大きすぎるとオーバーフロー
max(...x) 入力値の最大値 Math.max(1, 5, 3) = 5 引数がない → -Infinity
min(...x) 入力値の最小値 Math.min(1, 5, 3) = 1 引数がない → Infinity
pow(x, y) 入力値のべき乗(x ** y5) $x^y$ Math.pow(3, 4) = 81 xが-11,かつ
yがInfinity-InfinityNaN,
xが負の数で、かつyが整数ではない → NaN
sumPrecise([x₁, x₂...])6 7 入力値の合計 Math.sumPrecise([5,4,2]) = 11 反復可能オブジェクトが空の場合 → -0,
入力が反復可能でない場合 → TypeError

その他

メソッド 説明,式 例外
clz32(x) 入力値の32ビットバイナリ表現での先頭の0の個数 Math.clz32(1000) = 228 数値以外 → 32
random() 0以上1未満の乱数 Math.random() = 0.58265837583(例)
sign(x) 入力値が正の数なら1, 負の数なら-1 Math.sign(-3.14) = -1 0または-0 → そのまま返す

三角関数など

メソッド 説明,式 例外
sin(x) 入力値のsin $\sin$ $x$ Math.sin(1) = 0.8414709848078965 Infinity-Infinity
cos(x) 入力値のcos $\cos$ $x$ Math.cos(Math.PI) = -1 Infinityまたは-Infinity → `NaN
tan(x) 入力値のtan $\tan$ $x$ Math.tan(1) = 1.5574077246549023 Infinityまたは-InfinityNaN
asin(x) 入力値のarcsin $\sin^{-1}$ $x$ Math.asin(1) = 1.5707963267948966($\frac{\pi}{2}$) -1以上1以下ではない → NaN
acos(x) 入力値のarccos $\cos^{-1}$ $x$ Math.acos(1) = 0 -1以上1以下ではない → NaN
atan(x) 入力値のarctan $\tan^{-1}$ $x$ Math.atan(1) = 0.7853981633974483($\frac{\pi}{4}$)
sinh(x) 入力値のsinh $\frac{e^x-e^{-x}}{2}$ Math.sinh(1) = 1.1752011936438014
cosh(x) 入力値のcosh $\frac{e^x+e^{-x}}{2}$ Math.cosh(1) = 1.543080634815244 Infinityまたは-InfinityInfinity
tanh(x) 入力値のtanh $\frac{e^x-e^{-x}}{e^x+e^{-x}}$ Math.tanh(1) = 0.7615941559557649
asinh(x) 入力値のarcsinh $\ln$ $(x+\sqrt{x^2+1})$ Math.asinh(1) = 0.881373587019543
acosh(x) 入力値のarccosh $\ln$ $(x+\sqrt{x^2-1})$ Math.acosh(1) = 0 1未満 → NaN
atanh(x) 入力値のarctanh $\frac{1}{2}$ $\ln$ $(\frac{1+x}{1-x})$ Math.atanh(1) = Infinity -1以上1以下ではない → NaN
atan2(y, x) 点(0,0)から(x,y)の半直線と正のx軸の間のラジアン Math.atane(0, -1) = 3.141592653589793($\pi$)
  1. 要するに限られたビット数で2進数に近似するためのメソッドです

  2. $1.1_{(10)} \approx 00000001.00011001_{(2)} = 1.099609375_{(10)}$

  3. $1.05_{(10)} \approx 00000001.000011001100110011001100_{(2)} \approx 1.0499999523162842_{(10)}$

  4. オーバーフローなどにより必ずしもこの式の限りではないです

  5. ただし、Math.pow()**とは違って数値のみを受け入れます(数値以外を入力するとNaNが出力されます)

  6. 入力値としては、反復可能オブジェクトと言われる値が入ります

  7. ChromeやEdgeはこのメソッドに対応していないので注意してください

  8. 1000を32ビットバイナリ表現(2進数)で表現すると00000000000000000000001111101000となり、先頭の0の数は22個です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?