Math
是一个内置对象,它拥有一些数学常数属性和数学函数方法,Math
用于Number
类型,其不支持BigInt
。javascript
Math
不是一个函数对象,也就是说Math
不是一个构造器,Math
的全部属性与方法都是静态的,例如引用圆周率的写法是Math.PI
,Math
的常量是使用JavaScript
中的全精度浮点数来定义的,须要注意的是,不少Math
的函数都有一个精度,并且这个精度在不一样实现中也是不相同的,这意味着不一样的浏览器会给出不一样的结果,甚至在不一样的系统或架构下,相同的Js
引擎也会给出不一样的结果,另外三角函数sin()
、cos()
、tan()
、asin()
、acos()
、atan()
和atan2()
返回的值是弧度而非角度。若要转换,弧度除以Math.PI / 180
便可转换为角度,同理角度乘以这个数则能转换为弧度。java
console.log(Math.PI); // 3.141592653589793 console.log(Math.abs(-1)); // 1 console.log(Math.pow(2, 3)); // 8
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
: ½
的平方根,同时也是2
的平方根的倒数,约等于0.707
。Math.SQRT2
: 2
的平方根,约等于1.414
。Math.abs(x)
Math.abs(x)
函数返回指定数字x
的绝对值。git
console.log(Math.abs(-1)); // 1
Math.acos(x)
Math.acos()
返回一个数的反余弦值。
∀x∊[-1;1], Math.acos(x) = arccos(x) = the unique y∊[0;π] such that cos(y) = x
github
console.log(Math.acos(-1)); // 3.141592653589793
Math.acosh(x)
Math.acosh()
函数返回一个数的反双曲余弦值。
∀x≥1, Math.acosh(x) = arcosh(x) = the unique y≥0 such that cosh(y) = x
算法
console.log(Math.acosh(1)); // 0
Math.asin(x)
Math.asin()
方法返回一个数值的反正弦。
∀x∊[-1;1], Math.asin(x) = arcsin(x) = the unique y∊[- π/2 ; π/2 ] such that sin(y) = x
数组
console.log(Math.asin(0)); // 0
Math.asinh(x)
Math.asinh()
返回一个数值的反双曲正弦值。
Math.asinh(x) = arsinh(x) = the unique y such that sinh(y) = x
浏览器
console.log(Math.asinh(0)); // 0
Math.atan(x)
Math.atan()
函数返回一个数值的反正切。
Math.atan(x) = arctan(x) = the unique y∊[- π/2 ; π/2 ] such that tan(y) = x
架构
console.log(Math.atan(0)); // 0
Math.atanh(x)
Math.atanh()
函数返回一个数值反双曲正切值。
∀x∊(-1,1), Math.atanh(x) = arctanh(x) = the unique y such that tanh(y) = x
app
console.log(Math.atanh(0)); // 0
Math.atan2(y, x)
Math.atan2()
返回从原点(0,0)
到(x,y)
点的线段与x轴正方向之间的平面角度(弧度值),也就是Math.atan2(y,x)
。dom
console.log(Math.atan2(15, 90)); // 0.16514867741462683
Math.cbrt(x)
Math.cbrt()
函数返回任意数字的立方根。
console.log(Math.cbrt(27)); // 3
Math.ceil(x)
Math.ceil()
函数返回大于或等于一个给定数字的最小整数,即向上取整。
console.log(Math.ceil(6.6)); // 7
Math.clz32(x)
Math.clz32()
函数返回一个数字在转换成32
无符号整形数字的二进制形式后,开头的0
的个数, 好比1000000
转换成32
位无符号整形数字的二进制形式后是00000000000011110100001001000000
,开头的0
的个数是12
个,则Math.clz32(1000000)
返回12
。
console.log(Math.clz32(10)); // 28
Math.cos(x)
Math.cos()
函数返回一个数值的余弦值。
console.log(Math.clz32(10)); // 28
Math.cosh(x)
Math.cosh()
函数返回数值的双曲余弦函数。
console.log(Math.cosh(0)); // 1
Math.exp(x)
Math.exp()
函数返回e^x
,x
表示参数,e
是天然对数的底数约2.718281828459045
。
console.log(Math.exp(2)); // 7.38905609893065
Math.expm1(x)
Math.exp()
函数返回e^x -1
,x
表示参数,e
是天然对数的底数约2.718281828459045
。
console.log(Math.expm1(2)); // 6.38905609893065
Math.floor()
返回小于或等于一个给定数字的最大整数,即向下取整。
console.log(Math.floor(6.6)); // 6
Math.fround(doubleFloat)
Math.fround()
能够将任意的数字转换为离它最近的单精度浮点数形式的数字。JavaScript
内部使用64
位的双浮点数字,支持很高的精度。可是有时须要用32
位浮点数字,好比从一个Float32Array
读取值时,这时会产生混乱,检查一个64
位浮点数和一个32
位浮点数是否相等会失败,即便二个数字几乎如出一辙,要解决这个问题,可使用Math.fround()
来将64
位的浮点数转换为32
位浮点数,在内部JavaScript
继续把这个数字做为64
位浮点数看待,仅仅是在尾数部分的第23
位执行了舍入到偶”的操做,并将后续的尾数位设置为0
,若是数字超出32
位浮点数的范围,则返回Infinity
或-Infinity
。
// 数字1.5能够在二进制数字系统中精确表示,32位和64位的值相同 console.log(Math.fround(1.5) === 1.5); // true // 数字6.6却没法在二进制数字系统中精确表示,因此32位和64位的值是不一样的 console.log(Math.fround(6.6) === 6.6); // false // 在某些精度不高的场合下,能够经过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题 console.log(0.1 + 0.2 === 0.3); //false var equal = (v1, v2) => Math.fround(v1) === Math.fround(v2); console.log(equal(0.1 + 0.2, 0.3)); // true
Math.hypot([value1[,value2, ...]])
Math.hypot()
函数返回全部参数的平方和的平方根。本函数比Math.sqrt()
更简单也更快,只须要调用Math.hypot(v1, v2)
或Math.hypot(v1, v2, v3, v4, ...)
,其还避免了幅值过大的问题,Js
中最大的双精度浮点数是Number.MAX_VALUE = 1.797...e+308
,若是计算的数字比约1e154
大,计算其平方值会返回Infinity
,使计算的的结果出现问题。
console.log(Math.hypot(3, 4)); // 5
Math.imul(a, b)
Math.imul()
函数将两个参数分别转换为32
位整数,相乘后返回32
位结果,相似C
语言的32
位整数相乘。
console.log(Math.imul(0xffffffff, 1)); // -1
Math.log(x)
Math.log()
函数返回一个数的天然对数。
∀x>0, Math.log(x) = ln(x) = the unique y such that e^y = x
console.log(Math.log(Math.E)); // 1
Math.log10(x)
Math.log10()
函数返回一个数字以10
为底的对数。
console.log(Math.log10(100)); // 2
Math.log1p(x)
Math.log1p()
函数返回一个数字加1
后的天然对数, 既log(x+1)
。
console.log(Math.log1p(Math.E-1)); // 1
Math.log2(x)
Math.log2()
函数返回一个数字以2
为底的对数。
console.log(Math.log2(8)); // 3
Math.max(value1[,value2, ...])
Math.max()
函数返回一组数中的最大值。
console.log(Math.max(1, 2, 3)); // 3 // 在数组中应用 console.log(Math.max.apply(null, [1, 2, 3])); // 3 // 利用了apply的传参特性 console.log(Math.max(...[1, 2, 3])); // 3 // 利用了 ES6 Spread 操做符
Math.min([value1[,value2, ...]])
Math.min()
返回零个或更多个数值的最小值。
console.log(Math.min(1, 2, 3)); // 1 // 在数组中应用 console.log(Math.min.apply(null, [1, 2, 3])); // 1 // 利用了apply的传参特性 console.log(Math.min(...[1, 2, 3])); // 1 // 利用了 ES6 Spread 操做符
Math.pow(base, exponent)
Math.pow()
函数返回基数base
的指数exponent
次幂,即base^exponent
。
console.log(Math.pow(2, 3)); // 8
Math.random()
Math.random()
函数返回一个浮点数,伪随机数在范围从0
到小于1
,也就是说从0
(包括0
)往上,可是不包括1
,而后能够缩放到所需的范围,实现将初始种子选择到随机数生成算法,其不能被用户选择或重置。
console.log(Math.random()); // 0.20022678953392647 function randomInt(min=0, max=1) { // 生成随机整数 return min + ~~((max-min)*Math.random()); // min <= random < max } randomInt(1, 9); // 5
Math.round(x)
Math.round()
函数返回一个数字四舍五入后最接近的整数。
console.log(Math.round(0.5)); // 1
Math.sign(x)
Math.sign()
函数返回一个数字的符号, 指示数字是正数,负数仍是零。此函数共有5
种返回值, 分别是1, -1, 0, -0, NaN
表明的各是正数,负数,正零,负零,NaN
。
console.log(Math.sign(0.5)); // 1
Math.sin(x)
Math.sin()
函数返回一个数值的正弦值。
console.log(Math.sin(Math.PI / 2)); // 1
Math.sinh(x)
Math.sinh()
函数返回一个数字的双曲正弦值。
console.log(Math.sinh(0)); // 0
Math.sqrt(x)
Math.sqrt()
函数返回一个数的平方根。
∀x≥0, Math.sqrt(x) = x = the unique y≥0 such that y^2 = x
console.log(Math.sqrt(9)); // 3
Math.tan(x)
Math.tan()
方法返回一个数值的正切值。
console.log(Math.tan(0)); // 0
Math.tanh(x)
Math.tanh()
函数将会返回一个数的双曲正切函数值。
console.log(Math.tanh(0)); // 0
Math.trunc(value)
Math.trunc()
方法会将数字的小数部分去掉,只保留整数部分。
console.log(Math.trunc(1.1)); // 1
https://github.com/WindrunnerMax/EveryDay
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math