# Math

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

## 屬性

`Math.E` (en-US)

`Math.LN2` (en-US)
2 的自然對數，約為0.693。
`Math.LN10` (en-US)
10 的自然對數，約為2.303。
`Math.LOG2E` (en-US)

`Math.LOG10E` (en-US)

`Math.PI` (en-US)

`Math.SQRT1_2` (en-US)
1/2的平方根；也就是1除以2的平方根，約為 0.707。
`Math.SQRT2` (en-US)
2的平方根，約為 1.414。

## 方法

`Math.abs(x)`

`Math.acos(x)` (en-US)

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

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

`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)

`Math.ceil(x)`

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

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

`Math.expm1(x)` (en-US)

`Math.floor(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)

`Math.log1p(x)` (en-US)

`Math.log10(x)` (en-US)

`Math.log2(x)` (en-US)

`Math.max([x[, y[, …]]])`

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

`Math.pow(x, y)`

`Math.random()`

`Math.round(x)`

`Math.sign(x)` (en-US)

`Math.sin(x)` (en-US)

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

`Math.tan(x)` (en-US)

`Math.tanh(x)` (en-US)
Returns the hyperbolic tangent of a number.
`Math.toSource()`

`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.

``````/* 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()` (en-US), `log2()` (en-US), `log1p()` (en-US), `expm1()` (en-US), `cosh()` (en-US), `sinh()` (en-US), `tanh()` (en-US), `acosh()` (en-US), `asinh()` (en-US), `atanh()` (en-US), `hypot()` (en-US), `trunc()` (en-US), `sign()` (en-US), `imul()` (en-US), `fround()` (en-US), `cbrt()` (en-US) and `clz32()` (en-US) added.
ECMAScript (ECMA-262)
The definition of 'Math' in that specification.
Living Standard

## 瀏覽器相容性

BCD tables only load in the browser