Math

Math 是一個擁有數學常數及數學函數(非函式物件)屬性及方法的內建物件。

描述

不像其他的全域物件,Math 並非建構函式。所有 Math 的屬性及方法皆為靜態。你可以使用 Math.PI 來參考到圓周率 pi 的常數值,以及可以呼叫 Math.sin(x) 函式來計算三角函數正弦曲線 sine(x 為方法的引數)。常數是由 JavaScript 中實數的完整精度來定義。

屬性

Math.E (en-US)

歐拉常數 (此指自然常數) ,也是自然對數的底數,約為 2.718。

Math.LN2 (en-US)

2 的自然對數,約為 0.693。

Math.LN10 (en-US)

10 的自然對數,約為 2.303。

Math.LOG2E (en-US)

以 2 為底的 E 的對數,約為 1.443。

Math.LOG10E (en-US)

以 10 為底的 E 的對數,約為 0.434。

Math.PI (en-US)

一個圓的圓周和其直徑比值,約為 3.14159。

Math.SQRT1_2 (en-US)

1/2 的平方根;也就是 1 除以 2 的平方根,約為 0.707。

Math.SQRT2 (en-US)

2 的平方根,約為 1.414。

方法

備註: 三角函數 (sin(), cos(), tan(), asin(), acos(), atan(), atan2()) 的參數或回傳值的角度皆以弧度為單位。把角度乘上 (Math.PI / 180) 會得到弧度單位,將弧度除以該數則會轉換回一般所用的角度單位。

備註: 許多數學方法的精度是取決於實作方式的。這意味著不同的瀏覽器可能會得到不同的結果,甚至同一個 JS 引擎在不同的作業系統或架構上所得到的結果都有可能相異。

Math.abs(x) (en-US)

回傳 x 的絕對值。

Math.acos(x) (en-US)

回傳 x 的反餘弦值。

Math.acosh(x) (en-US)

Returns the hyperbolic arccosine of a number.

Math.asin(x) (en-US)

回傳 x 的反正弦值。

Math.asinh(x) (en-US)

Returns the hyperbolic arcsine of a number.

Math.atan(x) (en-US)

回傳 x 的反正切值。

Math.atanh(x) (en-US)

Returns the hyperbolic arctangent of a number.

Math.atan2(y, x) (en-US)

Returns the arctangent of the quotient of its arguments.

Math.cbrt(x) (en-US)

回傳 x 的立方根值。

Math.ceil(x)

回傳不小於 x 的最小整數值。

Math.clz32(x) (en-US)

Returns the number of leading zeroes of a 32-bit integer.

Math.cos(x) (en-US)

回傳 x 的餘弦值。

Math.cosh(x) (en-US)

Returns the hyperbolic cosine of a number.

Math.exp(x) (en-US)

回傳 E^x,x 為給定數值,E 為歐拉常數 (自然對數的底數)。

Math.expm1(x) (en-US)

回傳 exp(x) 減去 1 的值。

Math.floor(x)

回傳不大於 x 的最大整數值。

Math.fround(x) (en-US)

Returns the nearest single precision float representation of a number.

Math.hypot([x[, y[, …]]]) (en-US)

回傳參數平方之和的平方根。

Math.imul(x, y) (en-US)

Returns the result of a 32-bit integer multiplication.

Math.log(x) (en-US)

回傳 x 的自然對數值。

Math.log1p(x) (en-US)

回傳 1 + x 的自然對數值。

Math.log10(x) (en-US)

回傳以 10 為底,x 的對數值。

Math.log2(x) (en-US)

回傳以 2 為底,x 的對數值。

Math.max([x[, y[, …]]]) (en-US)

回傳給定數值中的最大值。

Math.min([x[, y[, …]]]) (en-US)

回傳給定數值中的最小值。

Math.pow(x, y) (en-US)

回傳 x 的 y 次方,也就是 x^y

Math.random()

回傳一個 0 到 1 之間的偽隨機值。

Math.round(x)

回傳 x 的四捨五入值。

Math.sign(x) (en-US)

回傳 x 的正負號,也就是回傳 x 的正負。

Math.sin(x) (en-US)

回傳 x 的正弦值。

Math.sinh(x) (en-US)

Returns the hyperbolic sine of a number.

Math.sqrt(x) (en-US)

回傳 x 的正平方根。

Math.tan(x) (en-US)

回傳 x 的正切值。

Math.tanh(x) (en-US)

Returns the hyperbolic tangent of a number.

Math.toSource() 非標準

回傳字串 "Math"

Math.trunc(x) (en-US)

Returns the integral part of the number x, removing any fractional digits.

擴充 Math 物件

As most of the built-in objects in JavaScript, the Math object can be extended with custom properties and methods. To extend the Math object, you do not use 'prototype'. Instead, you directly extend Math:

Math.propName = propValue;
Math.methodName = methodRef;

For instance, the following example adds a method to the Math object for calculating the greatest common divisor of a list of arguments.

js
/* Variadic function -- Returns the greatest common divisor of a list of arguments */
Math.gcd = function () {
  if (arguments.length == 2) {
    if (arguments[1] == 0) return arguments[0];
    else return Math.gcd(arguments[1], arguments[0] % arguments[1]);
  } else if (arguments.length > 2) {
    var result = Math.gcd(arguments[0], arguments[1]);
    for (var i = 2; i < arguments.length; i++)
      result = Math.gcd(result, arguments[i]);
    return result;
  }
};

Try it:

js
console.log(Math.gcd(20, 30, 15, 70, 40)); // `5`

規範

Specification
ECMAScript Language Specification
# sec-math-object

瀏覽器相容性

BCD tables only load in the browser

參見