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

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

## 描述

不像其他的全域物件，`Math`

並非建構函式。所有 `Math`

的屬性及方法皆為靜態。你可以使用 `Math.PI`

來參考到圓周率 pi 的常數值，以及可以呼叫 `Math.sin(x)`

函式來計算三角函數正弦曲線 sine（`x`

為方法的引數）。常數是由 JavaScript 中實數的完整精度來定義。

## 屬性

`Math.E`

- Euler's constant and the base of natural logarithms, approximately 2.718.
`Math.LN2`

- Natural logarithm of 2, approximately 0.693.
`Math.LN10`

- Natural logarithm of 10, approximately 2.303.
`Math.LOG2E`

- Base 2 logarithm of E, approximately 1.443.
`Math.LOG10E`

- Base 10 logarithm of E, approximately 0.434.
`Math.PI`

- Ratio of the circumference of a circle to its diameter, approximately 3.14159.
`Math.SQRT1_2`

- Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
`Math.SQRT2`

- Square root of 2, approximately 1.414.

## 方法

Note that the trigonometric functions (`sin()`

, `cos()`

, `tan()`

, `asin()`

, `acos()`

, `atan()`

, `atan2()`

) expect or return angles in radians. To convert radians to degrees, divide by `(Math.PI / 180)`

, and multiply by this to convert the other way.

Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.

`Math.abs(x)`

- Returns the absolute value of a number.
`Math.acos(x)`

- Returns the arccosine of a number.
`Math.acosh(x)`

- Returns the hyperbolic arccosine of a number.
`Math.asin(x)`

- Returns the arcsine of a number.
`Math.asinh(x)`

- Returns the hyperbolic arcsine of a number.
`Math.atan(x)`

- Returns the arctangent of a number.
`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)`

- Returns the cube root of a number.
`Math.ceil(x)`

- Returns the smallest integer greater than or equal to a number.
`Math.clz32(x)`

- Returns the number of leading zeroes of a 32-bit integer.
`Math.cos(x)`

- Returns the cosine of a number.
`Math.cosh(x)`

- Returns the hyperbolic cosine of a number.
`Math.exp(x)`

- Returns E
^{x}, where`x`is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm. `Math.expm1(x)`

- Returns subtracting 1 from
`exp(x)`

. `Math.floor(x)`

- Returns the largest integer less than or equal to a number.
`Math.fround(x)`

- Returns the nearest single precision float representation of a number.
`Math.hypot([x[, y[, …]]])`

- Returns the square root of the sum of squares of its arguments.
`Math.imul(x, y)`

- Returns the result of a 32-bit integer multiplication.
`Math.log(x)`

- Returns the natural logarithm (log
_{e}, also ln) of a number. `Math.log1p(x)`

- Returns the natural logarithm (log
_{e}, also ln) of`1 + x`

for a number x. `Math.log10(x)`

- Returns the base 10 logarithm of a number.
`Math.log2(x)`

- Returns the base 2 logarithm of a number.
`Math.max([x[, y[, …]]])`

- Returns the largest of zero or more numbers.
`Math.min([x[, y[, …]]])`

- Returns the smallest of zero or more numbers.
`Math.pow(x, y)`

- Returns base to the exponent power, that is,
`base`

.^{exponent} `Math.random()`

- Returns a pseudo-random number between 0 and 1.
`Math.round(x)`

- Returns the value of a number rounded to the nearest integer.
`Math.sign(x)`

- Returns the sign of the x, indicating whether x is positive, negative or zero.
`Math.sin(x)`

- Returns the sine of a number.
`Math.sinh(x)`

- Returns the hyperbolic sine of a number.
`Math.sqrt(x)`

- Returns the positive square root of a number.
`Math.tan(x)`

- Returns the tangent of a number.
`Math.tanh(x)`

- Returns the hyperbolic tangent of a number.
`Math.toSource()`

- Returns the string
`"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 |

## 瀏覽器相容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|

`E` | Yes | Yes | 1 | Yes | Yes | Yes |

`LN2` | Yes | Yes | 1 | Yes | Yes | Yes |

`LN10` | Yes | Yes | 1 | Yes | Yes | Yes |

`LOG2E` | Yes | Yes | 1 | Yes | Yes | Yes |

`LOG10E` | Yes | Yes | 1 | Yes | Yes | Yes |

`PI` | Yes | Yes | 1 | Yes | Yes | Yes |

`SQRT1_2` | Yes | Yes | 1 | Yes | Yes | Yes |

`SQRT2` | Yes | Yes | 1 | Yes | Yes | Yes |

`abs` | Yes | Yes | 1 | Yes | Yes | Yes |

`acos` | Yes | Yes | 1 | Yes | Yes | Yes |

`acosh` | 38 | Yes | 25 | No | 25 | 8 |

`asin` | Yes | Yes | 1 | Yes | Yes | Yes |

`asinh` | 38 | Yes | 25 | No | 25 | 8 |

`atan` | Yes | Yes | 1 | Yes | Yes | Yes |

`atan2` | Yes | Yes | 1 | Yes | Yes | Yes |

`atanh` | 38 | Yes | 25 | No | 25 | 8 |

`cbrt` | 38 | Yes | 25 | No | 25 | 8 |

`ceil` | Yes | Yes | 1 | Yes | Yes | Yes |

`clz32` | 38 | Yes | 31 | No | 25 | Yes |

`cos` | Yes | Yes | 1 | Yes | Yes | Yes |

`cosh` | 38 | Yes | 25 | No | 25 | 8 |

`exp` | Yes | Yes | 1 | Yes | Yes | Yes |

`expm1` | 38 | Yes | 25 | No | 25 | 8 |

`floor` | Yes | Yes | 1 | Yes | Yes | Yes |

`fround` | 38 | Yes | 26 | No | 25 | 8 |

`hypot` | 38 | Yes | 27 | No | 25 | 8 |

`imul` | 28 | Yes | 20 | No | 16 | 7 |

`log` | Yes | Yes | 1 | Yes | Yes | Yes |

`log1p` | 38 | Yes | 25 | No | 25 | 8 |

`log2` | 38 | Yes | 25 | No | 25 | 8 |

`log10` | 38 | Yes | 25 | No | 25 | 8 |

`max` | Yes | Yes | 1 | Yes | Yes | Yes |

`min` | Yes | Yes | 1 | Yes | Yes | Yes |

`pow` | Yes | Yes | 1 | Yes | Yes | Yes |

`random` | Yes | Yes | 1 | Yes | Yes | Yes |

`round` | Yes | Yes | 1 | Yes | Yes | Yes |

`sign` | 38 | Yes | 25 | No | 25 | 9 |

`sin` | Yes | Yes | 1 | Yes | Yes | Yes |

`sinh` | 38 | Yes | 25 | No | 25 | 8 |

`sqrt` | Yes | Yes | 1 | Yes | Yes | Yes |

`tan` | Yes | Yes | 1 | Yes | Yes | Yes |

`tanh` | 38 | Yes | 25 | No | 25 | 8 |

`trunc` | 38 | Yes | 25 | No | 25 | 8 |

Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|

`E` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`LN2` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`LN10` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`LOG2E` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`LOG10E` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`PI` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`SQRT1_2` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`SQRT2` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`abs` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`acos` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`acosh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`asin` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`asinh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`atan` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`atan2` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`atanh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`cbrt` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`ceil` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`clz32` | Yes | Yes | Yes | 31 | Yes | Yes | ? |

`cos` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`cosh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`exp` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`expm1` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`floor` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`fround` | Yes | Yes | Yes | 26 | Yes | 8 | ? |

`hypot` | Yes | Yes | Yes | 27 | Yes | 8 | ? |

`imul` | Yes | Yes | Yes | 20 | Yes | 7 | ? |

`log` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`log1p` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`log2` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`log10` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`max` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`min` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`pow` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`random` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`round` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`sign` | Yes | Yes | Yes | 25 | Yes | Yes | ? |

`sin` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`sinh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`sqrt` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`tan` | Yes | Yes | Yes | 4 | Yes | Yes | ? |

`tanh` | Yes | Yes | Yes | 25 | Yes | 8 | ? |

`trunc` | Yes | Yes | Yes | 25 | Yes | 8 | ? |