Math
Math
Math
是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。
描述
与其它全局对象不同的是, Math
不是一个构造器. Math
的所有属性和方法都是静态的. 你用到的常数pi可以用 Math.PI
表示,用 x
作参数 Math
.sin(x
)调用sin函数. 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.
方法
需要注意的是三角函数(sin()
, cos()
, tan()
,asin()
, acos()
, atan()
, atan2())是以弧度返回值的。可以通过除法(
Math.PI / 180)把弧度转换为角度,也可以通过其他方法来转换。
需要注意的是很多数学函数都有一个精度,并且精度在不同环境下也是不相同的。这就意味着不同的浏览器会给出不同的结果,甚至相同的 JS 引擎在不同的OS或者架构下也会给出不同的结果。
Math.abs(x)
返回x的绝对值.
扩展Math对象
由于JavaScript中的大多数内置Math
对象都可以使用自定义属性和方法进行扩展。为了扩展Math
对象,你不要使用“原型”而是直接扩展Math
:
Math.propName = propValue;
Math.methodName = methodRef;
例如,下面的示例向Math
对象添加一个方法来计算参数列表的最大公约数
。
/* Variadic function -- Returns the greatest common divisor of a list of arguments */
Math.gcd = function () {
var d = Math.min.apply(Math, arguments
for (let n = arguments.length, i = 0; d > 1 && n > i; arguments[i] % d === 0 ? i++ : (d--, i = 0)
return d;
}
尝试一下:
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. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LN2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LN10 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LOG2E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LOG10E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
PI | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
SQRT1_2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
SQRT2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
abs | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
acos | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
acosh | 38 | (Yes) | 25 | No | 25 | 7.1 |
asin | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
asinh | 38 | (Yes) | 25 | No | 25 | 7.1 |
atan | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
atan2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
atanh | 38 | (Yes) | 25 | No | 25 | 7.1 |
cbrt | 38 | (Yes) | 25 | No | 25 | 7.1 |
ceil | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
clz32 | 38 | (Yes) | 31 | No | 25 | (Yes) |
cos | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
cosh | 38 | (Yes) | 25 | No | 25 | 7.1 |
exp | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
expm1 | 38 | (Yes) | 25 | No | 25 | 7.1 |
floor | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
fround | 38 | (Yes) | 26 | No | 25 | 7.1 |
hypot | 38 | (Yes) | 27 | No | 25 | 7.1 |
imul | 28 | (Yes) | 20 | No | 16 | 7 |
log | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
log1p | 38 | (Yes) | 25 | No | 25 | 7.1 |
log2 | 38 | (Yes) | 25 | No | 25 | 7.1 |
log10 | 38 | (Yes) | 25 | No | 25 | 7.1 |
max | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
min | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
pow | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
random | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
round | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
sign | 38 | (Yes) | 25 | No | 25 | 9 |
sin | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
sinh | 38 | (Yes) | 25 | No | 25 | 7.1 |
sqrt | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
tan | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
tanh | 38 | (Yes) | 25 | No | 25 | 7.1 |
trunc | 38 | (Yes) | 25 | No | 25 | 7.1 |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LN2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LN10 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LOG2E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
LOG10E | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
PI | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
SQRT1_2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
SQRT2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
abs | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
acos | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
acosh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
asin | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
asinh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
atan | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
atan2 | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
atanh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
cbrt | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
ceil | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
clz32 | (Yes) | (Yes) | (Yes) | 31 | No | (Yes) | (Yes) |
cos | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
cosh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
exp | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
expm1 | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
floor | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
fround | (Yes) | (Yes) | (Yes) | 26 | No | (Yes) | 8 |
hypot | (Yes) | (Yes) | (Yes) | 27 | No | (Yes) | 8 |
imul | (Yes) | (Yes) | (Yes) | 20 | No | (Yes) | 7 |
log | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
log1p | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
log2 | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
log10 | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
max | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
min | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
pow | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
random | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
round | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
sign | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | (Yes) |
sin | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
sinh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
sqrt | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
tan | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
tanh | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |
trunc | (Yes) | (Yes) | (Yes) | 25 | No | (Yes) | 8 |