数学オブジェクト

「Math」は、さまざまな数学関数を提供するネイティブ JavaScript オブジェクトです。このオブジェクトはコンストラクターではないため、すべてのプロパティとメソッドを Math オブジェクトで呼び出す必要があります。

静的プロパティ

Math オブジェクトの静的プロパティ。次の数学定数を提供します。

  • Math.E: 定数 e
  • Math.LN2: 2 の自然対数。
  • Math.LN10: 10 の自然対数。
  • Math.LOG2E: e の底 2 の対数。
  • Math.LOG10E: e の底 10 の対数。
  • Math.PI: 定数 π
  • Math.SQRT1_2: 0.5 の平方根。
  • Math.SQRT2: 2 の平方根。
数学 E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 // 1.4142135623730951

これらのプロパティは読み取り専用であり、変更できません。

静的メソッド

Math オブジェクトは次の静的メソッドを提供します。

  • Math.abs(): 絶対値
  • Math.ceil(): 切り上げ
  • Math.floor(): 切り捨て
  • Math.max(): 最大値
  • Math.min(): 最小値
  • Math.pow(): べき乗演算
  • Math.sqrt(): 平方根
  • Math.log(): 自然対数
  • Math.exp(): eの指数
  • Math.round(): 丸め
  • Math.random(): 乱数

Math.abs()

Math.abs メソッドは引数値の絶対値を返します。

Math.abs(1) // 1
Math.abs(-1) // 1

Math.max()、Math.min()

Math.max メソッドはパラメータの最大値を返し、Math.min メソッドは最小値を返します。パラメータが空の場合、Math.minInfinity を返し、Math.max-Infinity を返します。

Math.max(2, -1, 5) // 5
Math.min(2, -1, 5) // -1
Math.min() // 無限大
Math.max() // -無限大

Math.floor()、Math.ceil()

Math.floor メソッドは、引数値以下の最大の整数 (フロア値) を返します。

Math.floor(3.2) // 3
Math.floor(-3.2) // -4

Math.ceil メソッドは、引数値以上の最小の整数 (上限値) を返します。

Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3

これら 2 つのメソッドを組み合わせて、常に値の整数部分を返す関数を実装できます。

関数 ToInteger(x) {
  x = 数値(x);
  戻り値 x < 0 ? Math.ceil(x) : Math.floor(x);
}

ToInteger(3.2) // 3
ToInteger(3.5) // 3
ToInteger(3.8) // 3
ToInteger(-3.2) // -3
ToInteger(-3.5) // -3
ToInteger(-3.8) // -3

上記のコードでは、ToInteger 関数は、値が正か負かに関係なく、常に値の整数部分を返します。

Math.round()

丸めには Math.round メソッドが使用されます。

Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1

// と同等
Math.floor(x + 0.5)

負の数 (主に 0.5) を扱うことに注意してください。

Math.round(-1.1) // -1
Math.round(-1.5) // -1
Math.round(-1.6) // -2

Math.pow()

Math.pow メソッドは、最初の引数を底、2 番目の引数を指数として累乗した値を返します。

// 2 ** 2 に相当
Math.pow(2, 2) // 4
// 2 ** 3 に相当
Math.pow(2, 3) // 8

ここでは円の面積を計算する方法を説明します。

変数半径 = 20;
var area = Math.PI * Math.pow(radius, 2);

Math.sqrt()

Math.sqrt メソッドは引数値の平方根を返します。引数が負の値の場合、「NaN」が返されます。

Math.sqrt(4) // 2
Math.sqrt(-4) // NaN

Math.log()

Math.log メソッドは、底が e の自然対数を返します。

Math.log(Math.E) // 1
Math.log(10) // 2.302585092994046

底 10 の対数を計算したい場合は、まず Math.log を使用して自然対数を見つけ、それを Math.LN10 で割って底 2 の対数を求めます。 「数学.LN2」。

Math.log(100)/Math.LN10 // 2
Math.log(8)/Math.LN2 // 3

Math.exp()

Math.exp メソッドは、引数の累乗した定数 e を返します。

Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668

Math.random()

Math.random() は 0 から 1 までの擬似乱数を返します。これは 0 に等しい場合もありますが、1 未満である必要があります。

Math.random() // 0.7151307314634323

任意の範囲の乱数生成関数は次のとおりです。

関数 getRandomArbitrary(min, max) {
  return Math.random() * (最大値 - 最小値) + 最小値;
}

getRandomArbitrary(1.5, 6.5)
// 2.4942810038223864

任意の範囲に対するランダムな整数生成関数は次のとおりです。

関数 getRandomInt(min, max) {
  return Math.floor(Math.random() * (最大 - 最小 + 1)) + 最小;
}

getRandomInt(1, 6) // 5

ランダムな文字を返す例は次のとおりです。

関数random_str(長さ) {
  var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  アルファベット += 'abcdefghijklmnopqrstuvwxyz';
  アルファベット += '0123456789-_';
  var str = '';
  for (var i = 0; i < length; ++i) {
    var rand = Math.floor(Math.random() * ALPHABET.length);
    str += ALPHABET.substring(rand, rand + 1);
  }
  文字列を返します。
}

random_str(6) // "NdQKOr"

上記のコードでは、random_str 関数はパラメータとして整数を受け取り、変数 ALPHABET 内のランダムな文字で構成される指定された長さの文字列を返します。

三角関数法

Math オブジェクトは、一連の三角関数メソッドも提供します。

  • Math.sin(): パラメータのサインを返します (パラメータはラジアン値です)
  • Math.cos(): パラメータのコサインを返します (パラメータはラジアン値です)
  • Math.tan(): パラメータのタンジェントを返します (パラメータはラジアン値です)。
  • Math.asin(): パラメータの逆正弦を返します (戻り値はラジアン値です)
  • Math.acos(): パラメータの逆余弦を返します (戻り値はラジアン値です)
  • Math.atan(): パラメータの逆正接を返します (戻り値はラジアン値です)
Math.sin(0) // 0
Math.cos(0) // 1
Math.tan(0) // 0

Math.sin(Math.PI / 2) // 1

Math.asin(1) // 1.5707963267948966
Math.acos(1) // 0
Math.atan(1) // 0.7853981633974483

作者: wangdoc

アドレス: https://wangdoc.com/

ライセンス: クリエイティブ・コモンズ 3.0