Math

Math 是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。

Math 适用于 BigInt

描述

与其他全局对象不同的是,Math 不是一个构造器。 Math 的所有属性与方法都是静态的。引用圆周率的写法是 Math.PI,调用正余弦函数的写法是 Math.sin(x)x 是要传入的参数。Math 的常量是使用 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的平方根, 约等于 0.707。
Math.SQRT2
2的平方根,约等于 1.414。
Properties inherited from Object:

方法

需要注意的是,sin(), cos(), tan(), asin(), acos(), atan(), atan2() 是以弧度返回值的。可以通过除法(Math.PI / 180)或其他方法把弧度转换为角度。

需要注意的是,很多数学函数都有一个精度,并且精度在不同实现中也是不相同的。这就意味着不同的浏览器会给出不同的结果,甚至相同的 JS 引擎在不同的系统或架构下也会给出不同的结果。

Math.abs(x)
返回 x 的绝对值。
Math.acos(x)
返回 x 的反余弦值。
Math.acosh(x)
返回 x 的反双曲余弦值。
Math.asin(x)
返回 x 的反正弦值。
Math.asinh(x)
返回 x 的反双曲正弦值。
Math.atan(x)
以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
Math.atanh(x)
返回 x 的反双曲正切值。
Math.atan2(y, x)
返回 y/x 的反正切值。
Math.cbrt(x)
返回 x 的立方根。
Math.ceil(x)
返回 x 向上取整后的值。
Math.clz32(x)
返回一个 32 位整数的前导零的数量。
Math.cos(x)
返回 x 的余弦值。
Math.cosh(x)
返回 x 的双曲余弦值。
Math.exp(x)
返回 Ex,当x为参数,E 是欧拉常数(2.718...),自然对数的底数。
Math.expm1(x)
返回 exp(x) - 1 的值。
Math.floor(x)
返回小于 x 的最大整数。
Math.fround(x)
返回数字的最接近的单精度浮点型表示。
Math.hypot([x[, y[, …]]])
返回其参数平方和的平方根。
Math.imul(x, y)
返回 32 位整数乘法的结果。
Math.log(x)
返回一个数的自然对数(log e,即 ln)。
Math.log1p(x)
返回 1 加上一个数字的的自然对数(log e,即 ln)。
Math.log10(x)
返回以 10 为底数的 x 的对数。
Math.log2(x)
返回以 2 为底数的 x 的对数。
Math.max([x[, y[, …]]])
返回 0 个到多个数值中最大值。
Math.min([x[, y[, …]]])
返回 0 个到多个数值中最小值。
Math.pow(x, y)
返回 x 的 y 次幂。
Math.random()
返回 0 到 1 之间的伪随机数。
Math.round(x)
返回四舍五入后的整数。
Math.sign(x)
返回 x 的符号函数, 判定 x 是正数,负数还是 0。
Math.sin(x)
返回正弦值。
Math.sinh(x)
返回 x 的双曲正弦值。
Math.sqrt(x)
返回 x 的平方根。
Math.tan(x)
返回 x 的正切值。
Math.tanh(x)
返回 x 的双曲正切值。
Math.toSource()
返回字符串 "Math"
Math.trunc(x)
返回 x 的整数部分,直接去除小数点及之后的部分。

拓展 Math 对象

As with 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;
    }
};

试试运行下面的代码:

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

规范

规范 状态 说明
ECMAScript 1st Edition (ECMA-262) Standard 初始定义。在 JavaScript 1.1 中实现。
ECMAScript 5.1 (ECMA-262)
Math
Standard
ECMAScript 2015 (6th Edition, ECMA-262)
Math
Standard New methods clz32() added.
ECMAScript Latest Draft (ECMA-262)
Math
Draft

浏览器兼容性

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
E Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
LN2 Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
LN10 Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
LOG2E Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
LOG10E Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
PI Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
SQRT1_2 Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
SQRT2 Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
abs Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
acos Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
acosh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
asin Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
asinh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
atan Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
atan2 Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
atanh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
cbrt Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
ceil Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
clz32 Chrome Full support 38 Edge Full support 12 Firefox Full support 31 IE No support No Opera Full support 25 Safari Full support Yes WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 31 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support 0.12
cos Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
cosh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
exp Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
expm1 Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
floor Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
fround Chrome Full support 38 Edge Full support 12 Firefox Full support 26 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 26 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
hypot Chrome Full support 38 Edge Full support 12 Firefox Full support 27 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 27 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
imul Chrome Full support 28 Edge Full support 12 Firefox Full support 20 IE No support No Opera Full support 16 Safari Full support 7 WebView Android Full support Yes Chrome Android Full support 28 Firefox Android Full support 20 Opera Android Full support Yes Safari iOS Full support 7 Samsung Internet Android Full support Yes nodejs Full support 0.12
log Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
log1p Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
log2 Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
log10 Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
max Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
min Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
pow Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
random Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
round Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
sign Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 9 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support 0.12
sin Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
sinh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
sqrt Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
tan Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
tanh Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12
trunc Chrome Full support 38 Edge Full support 12 Firefox Full support 25 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 25 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

参见