翻譯不完整。請協助 翻譯此英文文件

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

描述

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

屬性

Math.E
歐拉常數 (此指自然常數) ,也是自然對數的底數,約為2.718。
Math.LN2
2 的自然對數,約為0.693。
Math.LN10
10 的自然對數,約為2.303。
Math.LOG2E
以 2 為底的 E 的對數,約為1.443。
Math.LOG10E
以 10 為底的 E 的對數,約為0.434。
Math.PI
一個圓的圓周和其直徑比值,約為 3.14159。
Math.SQRT1_2
1/2的平方根;也就是1除以2的平方根,約為 0.707。
Math.SQRT2
2的平方根,約為 1.414。

方法

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

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

Math.abs(x)
回傳 x 的絕對值。
Math.acos(x)
回傳 x 的反餘弦值。
Math.acosh(x)
Returns the hyperbolic arccosine of a number.
Math.asin(x)
回傳 x 的反正弦值。
Math.asinh(x)
Returns the hyperbolic arcsine of a number.
Math.atan(x)
回傳 x 的反正切值。
Math.atanh(x)
Returns the hyperbolic arctangent of a number.
Math.atan2(y, x)
Returns the arctangent of the quotient of its arguments.
Math.cbrt(x)
回傳 x 的立方根值。
Math.ceil(x)
回傳不小於 x 的最小整數值。
Math.clz32(x)
Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x)
回傳 x 的餘弦值。
Math.cosh(x)
Returns the hyperbolic cosine of a number.
Math.exp(x)
回傳 Ex,x 為給定數值,E 為歐拉常數 (自然對數的底數)。
Math.expm1(x)
回傳 exp(x) 減去1的值。
Math.floor(x)
回傳不大於 x 的最大整數值。
Math.fround(x)
Returns the nearest single precision float representation of a number.
Math.hypot([x[, y[, …]]])
回傳參數平方之和的平方根。
Math.imul(x, y)
Returns the result of a 32-bit integer multiplication.
Math.log(x)
回傳 x 的自然對數值。
Math.log1p(x)
回傳 1 + x 的自然對數值。
Math.log10(x)
回傳以 10 為底,x 的對數值。
Math.log2(x)
回傳以 2 為底,x 的對數值。
Math.max([x[, y[, …]]])
回傳給定數值中的最大值。
Math.min([x[, y[, …]]])
回傳給定數值中的最小值。
Math.pow(x, y)
回傳 x 的 y 次方,也就是 xy
Math.random()
回傳一個 0 到 1 之間的偽隨機值。
Math.round(x)
回傳 x 的四捨五入值。
Math.sign(x)
回傳 x 的正負號,也就是回傳 x 的正負。
Math.sin(x)
回傳 x 的正弦值。
Math.sinh(x)
Returns the hyperbolic sine of a number.
Math.sqrt(x)
回傳 x 的正平方根。
Math.tan(x)
回傳 x 的正切值。
Math.tanh(x)
Returns the hyperbolic tangent of a number.
Math.toSource()
回傳字串 "Math"
Math.trunc(x)
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.

/* 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:

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

規範

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.1.
ECMAScript 5.1 (ECMA-262)
The definition of 'Math' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Math' in that specification.
Standard New methods log10(), log2(), log1p(), expm1(), cosh(), sinh(), tanh(), acosh(), asinh(), atanh(), hypot(), trunc(), sign(), imul(), fround(), cbrt() and clz32() added.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Math' in that specification.
Draft  

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
EChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
LN2Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
LN10Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
LOG2EChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
LOG10EChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
PIChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
SQRT1_2Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
SQRT2Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
absChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
acosChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
acoshChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
asinChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
asinhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
atanChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
atan2Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
atanhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
cbrtChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
ceilChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
clz32Chrome Full support 38Edge Full support YesFirefox Full support 31IE No support NoOpera Full support 25Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 31Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
cosChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
coshChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
expChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
expm1Chrome Full support 38Edge Full support YesFirefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
floorChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
froundChrome Full support 38Edge Full support 12Firefox Full support 26IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
hypotChrome Full support 38Edge Full support 12Firefox Full support 27IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 27Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
imulChrome Full support 28Edge Full support 12Firefox Full support 20IE No support NoOpera Full support 16Safari Full support 7WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 20Opera Android Full support YesSafari iOS Full support 7Samsung Internet Android Full support Yesnodejs Full support 0.12
logChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
log1pChrome Full support 38Edge Full support YesFirefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
log2Chrome Full support 38Edge Full support YesFirefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
log10Chrome Full support 38Edge Full support YesFirefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
maxChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
minChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
powChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
randomChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
roundChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
signChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
sinChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
sinhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
sqrtChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
tanChrome Full support YesEdge Full support 12Firefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
tanhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
truncChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

參見

文件標籤與貢獻者

此頁面的貢獻者: hmysjiang, mdnwebdocs-bot, jackblackevo, fscholz
最近更新: hmysjiang,