Math.abs(x) 函数返回一个数字的绝对值。
Math.abs(-Infinity);// Infinity
Math.abs(-1);// 1
Math.abs(-0);// 0
Math.abs(0);// 0
Math.abs(1);// 1
Math.abs(Infinity);// Infinity
Math.max() 函数返回作为输入参数的最大数字,如果没有参数,则返回 -Infinity。
Math.max(10,20);// 20
Math.max(-10,-20);// -10
Math.max(-10,20);// 20
Math.min() 函数返回作为输入参数的数字中最小的一个,如果没有参数,则返回 Infinity。
Math.min()
Math.min(value0)
Math.min(value0, value1)
Math.min(value0, value1,/* … ,*/ valueN)
Math.pow() 函数返回基数(base)的指数(exponent)次幂
console.log(Math.pow(7, 3));
// Expected output: 343
console.log(Math.pow(4, 0.5));
// Expected output: 2
console.log(Math.pow(7, -2));
// Expected output: 0.02040816326530612
// (1/49)
console.log(Math.pow(-7, 0.5));
// Expected output: NaN
Math.random() 函数返回一个浮点数,伪随机数在范围从0 到小于1,也就是说,从 0(包括 0)往上,但是不包括 1(排除 1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。
得到一个两数之间的随机数
function getRandomArbitrary(min, max){return Math.random()*(max - min)+ min;}
得到一个两数之间的随机整数
function getRandomInt(min, max){
min = Math.ceil(min);
max = Math.floor(max);return Math.floor(Math.random()*(max - min))+ min;//不含最大值,含最小值}
Math.round() 函数返回一个数字四舍五入后最接近的整数。
x = Math.round(20.49);//20
x = Math.round(20.5);//21
x = Math.round(-20.5);//-20
x = Math.round(-20.51);//-21
Math.sqrt() 函数返回一个数的平方根
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0
Math.acos() 返回一个数的反余弦值(单位为弧度)
Math.acos(-2);// NaN
Math.acos(-1);// 3.141592653589793
Math.acos(0);// 1.5707963267948966
Math.acos(0.5);// 1.0471975511965979
Math.acos(1);// 0
Math.acos(2);// NaN
Math.acosh() 函数返回一个数的反双曲余弦值
Math.acosh(-1);// NaN
Math.acosh(0);// NaN
Math.acosh(0.5);// NaN
Math.acosh(1);// 0
Math.acosh(2);// 1.3169578969248166
Math.asin() 方法返回一个数值的反正弦(单位为弧度)
Math.asin(-2);// NaN
Math.asin(-1);// -1.5707963267948966 (-pi/2)
Math.asin(0);// 0
Math.asin(0.5);// 0.5235987755982989
Math.asin(1);// 1.5707963267948966 (pi/2)
Math.asin(2);// NaN
Math.asinh() 返回一个数值的反双曲正弦值,
Math.asinh(1);// 0.881373587019543
Math.asinh(0);// 0
Math.atan() 函数返回一个数值的反正切(以弧度为单位)
Math.atan(1);// 0.7853981633974483
Math.atan(0);// 0
Math.atan2() 返回从原点 (0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度 (弧度值),也就是 Math.atan2(y,x)
Math.atan2(90,15)// 1.4056476493802699
Math.atan2(15,90)// 0.16514867741462683
Math.atan2( ±0,-0)// ±PI.
Math.atan2( ±0,+0)// ±0.
Math.atan2( ±0,-x )// ±PI for x > 0.
Math.atan2( ±0, x )// ±0 for x > 0.
Math.atan2(-y, ±0)// -PI/2 for y > 0.
Math.atan2( y, ±0)// PI/2 for y > 0.
Math.atan2( ±y,-Infinity)// ±PI for finite y > 0.
Math.atan2( ±y,+Infinity)// ±0 for finite y > 0.
Math.atan2( ±Infinity, x )// ±PI/2 for finite x.
Math.atan2( ±Infinity,-Infinity)// ±3*PI/4.
Math.atan2( ±Infinity,+Infinity)// ±PI/4.
Math.atanh() 函数返回一个数值反双曲正切值
Math.atanh(-2);// NaN
Math.atanh(-1);// -Infinity
Math.atanh(0);// 0
Math.atanh(0.5);// 0.5493061443340548
Math.atanh(1);// Infinity
Math.atanh(2);// NaN
Math.cbrt() 函数返回任意数字的立方根。
Math.cbrt(NaN);// NaN
Math.cbrt(-1);// -1
Math.cbrt(-0);// -0
Math.cbrt(-Infinity);// -Infinity
Math.cbrt(0);// 0
Math.cbrt(1);// 1
Math.cbrt(Infinity);// Infinity
Math.cbrt(null);// 0
Math.cbrt(2);// 1.2599210498948732
Math.ceil() 函数总是四舍五入并返回大于等于给定数字的最小整数。
Math.ceil(-Infinity);// -Infinity
Math.ceil(-7.004);// -7
Math.ceil(-4);// -4
Math.ceil(-0.95);// -0
Math.ceil(-0);// -0
Math.ceil(0);// 0
Math.ceil(0.95);// 1
Math.ceil(4);// 4
Math.ceil(7.004);// 8
Math.ceil(Infinity);// Infinity
Math.clz32() 函数返回一个数字在转换成 32 无符号整形数字的二进制形式后,开头的 0 的个数,比如 1000000 转换成 32 位无符号整形数字的二进制形式后是 00000000000011110100001001000000, 开头的 0 的个数是 12 个,则 Math.clz32(1000000) 返回 12.
Math.clz32(1)// 31
Math.clz32(1000)// 22
Math.clz32()// 32[NaN,Infinity,-Infinity,0,-0,null,undefined,"foo",{},[]].filter(function(n){return Math.clz32(n)!==32})// []
Math.clz32(true)// 31
Math.clz32(3.5)// 30
Math.cos() 函数返回一个数值的余弦值。
Math.cos(0);// 1
Math.cos(1);// 0.5403023058681398
Math.cos(Math.PI);// -1
Math.cos(2* Math.PI);// 1
Math.cosh() 函数返回数值的双曲余弦函数
Math.cosh(0);// 1
Math.cosh(1);// 1.5430806348152437
Math.cosh(-1);// 1.5430806348152437
Math.exp() 函数返回 e^x,x 表示参数
Math.exp(-1);// 0.36787944117144233
Math.exp(0);// 1
Math.exp(1);// 2.718281828459045
Math.expm1() 函数返回 E^x - 1, 其中 x 是该函数的参数,E 是自然对数的底数 2.718281828459045。
Math.expm1(-Infinity);// -1
Math.expm1(-1);// -0.6321205588285577
Math.expm1(-0);// -0
Math.expm1(0);// 0
Math.expm1(1);// 1.718281828459045
Math.expm1(Infinity);// Infinity
Math.floor() 函数总是返回小于等于一个给定数字的最大整数
Math.floor(-Infinity);// -Infinity
Math.floor(-45.95);// -46
Math.floor(-45.05);// -46
Math.floor(-0);// -0
Math.floor(0);// 0
Math.floor(4);// 4
Math.floor(45.05);// 45
Math.floor(45.95);// 45
Math.floor(Infinity);// Infinity
Math.fround() 可以将任意的数字转换为离它最近的单精度浮点数形式的数字。
Math.fround(1.5);// 1.5
Math.fround(1.5)===1.5;// true
Math.hypot() 函数返回所有参数的平方和的平方根,
Math.hypot(3,4);// 5
Math.hypot(3,4,5);// 7.0710678118654755
Math.hypot();// 0
Math.hypot(NaN);// NaN
Math.hypot(3,4,'foo');// NaN, +'foo' => NaN
Math.hypot(3,4,'5');// 7.0710678118654755, +'5' => 5
Math.hypot(-3);// 3, the same as Math.abs(-3)
Math.imul()
该函数将两个参数分别转换为 32 位整数,相乘后返回 32 位结果,类似 C 语言的 32 位整数相乘。
Math.imul(2,4)// 8
Math.imul(-1,8)// -8
Math.imul(-2,-2)// 4
Math.imul(0xffffffff,5)//-5
Math.imul(0xfffffffe,5)//-10
Math.log() 函数返回一个数的自然对数
Math.log(-1);// NaN, out of range
Math.log(0);// -Infinity
Math.log(1);// 0
Math.log(10);// 2.302585092994046
Math.log10() 函数返回一个数字以 10 为底的对数。
Math.log10(10)// 1
Math.log10(100)// 2
Math.log10("100")// 2
Math.log10(1)// 0
Math.log10(0)// -Infinity
Math.log10(-2)// NaN
Math.log10("foo")// NaN
Math.log1p() 函数返回一个数字加 1 后的自然对数 (底为 E), 既log(x+1).
Math.log1p(Math.E-1)// 1
Math.log1p(0)// 0
Math.log1p("0")// 0
Math.log1p(-1)// -Infinity
Math.log1p(-2)// NaN
Math.log1p("foo")// NaN
Math.log2() 函数返回一个数字以 2 为底的对数。
Math.log2(2)// 1
Math.log2(1024)// 10
Math.log2(1)// 0
Math.log2(0)// -Infinity
Math.log2(-2)// NaN
Math.log2("1024")// 10
Math.log2("foo")// NaN
Math.sign() 函数返回一个数字的符号,指示数字是正数,负数还是零。
Math.sign(3);// 1
Math.sign(-3);// -1
Math.sign("-3");// -1
Math.sign(0);// 0
Math.sign(-0);// -0
Math.sign(NaN);// NaN
Math.sign("foo");// NaN
Math.sign();// NaN
Math.sin() 函数返回一个数值的正弦值。
Math.sin(0);// 0
Math.sin(1);// 0.8414709848078965
Math.sin(Math.PI/2);// 1
Math.sinh() 函数返回一个数字 (单位为角度) 的双曲正弦值。
Math.sinh(0)// 0
Math.sinh(1)// 1.1752011936438014
Math.sinh("-1")// -1.1752011936438014
Math.sinh("foo")// NaN
Math.tan() 方法返回一个数值的正切值。
function getTan(x) {
return Math.tan(x);
}
Math.tanh() 函数将会返回一个数的双曲正切函数值
Math.tanh(0); // 0
Math.tanh(Infinity); // 1
Math.tanh(1); // 0.7615941559557649
Math.trunc() 方法会将数字的小数部分去掉,只保留整数部分。
Math.trunc(13.37)// 13
Math.trunc(42.84)// 42
Math.trunc(0.123)// 0
Math.trunc(-0.123)// -0
Math.trunc("-1.123")// -1
Math.trunc(NaN)// NaN
Math.trunc("foo")// NaN
Math.trunc()// NaN
参考文献:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
1292

被折叠的 条评论
为什么被折叠?



