JS内置对象 - Math
属性:
Math.E属性表示自然对数的底数(或称为基数),e,约等于2.718
Math.LN10属性表示10的自然对数,约为2.302
Math.PI属性表示一个圆的周长与直径的比例,约为3.1415926
Math.LOG10E属性表示以10位底数,e的对数,约为0.434;
Math.LOG2E属性表示以2为底数,e的对数,约为1.442;
Math.SQRT1_2:表示1/2的平方根,约为0.707;
Math.SQRT2:表示2的平方根,约为1.414;
方法:
1.Math.abs(x)
函数返回一个数字的绝对值;
参数:
x为一个数字
返回值:
x的绝对值.如果x是负数(包括-0),则返回-x.否则返回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.abs()将其参数强制转换为数字.无法强制转换的值变成NaN,然后abs()也会返回NaN
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN
放入函数:
conseole.log(Math.abs(-2))
// expected output : 2
function difference(a, b) {
return Math.abs(a - b);
}
console.log(difference(3, 5));
// expected output: 2
console.log(difference(5, 3));
// expected output: 2
console.log(difference(1.23456, 7.89012));
// expected output: 6.6555599999999995
2.Math.atan()
该函数返回一个数值的正反切
返回值:atan 返回一个- π / 2 到 π /2 弧度之间的数值;
Math.atan(1); // 0.7853981633974483
Math.atan(0); // 0
3.Math.atan2()
返回值:
Math.atan2()返回从原点(0,0)到(x,y)点的线段与x轴之间的平面角度(弧度值),也就是Math.atan2(y,x)
简述
atan2方法返回一个-pi 到 pi 之间的数值,表示点(x,y)的偏移角度.这是一个逆时针角度.以弧度为单位正x轴和点(x,y)与原点连线之间.注意此函数参数为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.
4.Math.acos
返回值:
Math.acos()返回一个数的反余弦值(单位为弧度),即:
描述:
acos方法以-1 到 1 的一个数为参数,返回一个0到pi(弧度)的数值.如果传入的参数值超出了限定的范围,将返回NaN;
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
5.Math.asin()
返回值:
asin方法返回一个数值的反正弦(单位为弧度);
该方法接收-1 到 1之间的数值作为参数,返回一个介于 - π/2 到 π/2弧度的数值.如果接受的参数超出范围,则返回NaN
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.570796326794897 (pi/2)
Math.asin(2); // NaN
7.Math.cos()
**返回值:**函数返回一个数值的余弦值
cos方法返回一个-1 到 1之间的数值,表示角度(单位:弧度)的余弦值.
Math.cos(0); // 1
Math.cos(1); // 0.5403023058681398
Math.cos(Math.PI); // -1
Math.cos(2 * Math.PI); // 1
8.Math.floor()
**返回值:**该函数总返回小于等于一个给定数字的最大整数;
//实例:
console.log(Math.floor(5.95));
// expected output: 5
console.log(Math.floor(5.05));
// expected output: 5
console.log(Math.floor(5));
// expected output: 5
console.log(Math.floor(-5.05));
// expected output: -6
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
9.Math.ceil();
**返回值:**总是四舍五入并返回大于等于给数字的最小整数
console.log(Math.ceil(.95));
// expected output: 1
console.log(Math.ceil(4));
// expected output: 4
console.log(Math.ceil(7.004));
// expected output: 8
console.log(Math.ceil(-7.004));
// expected output: -7
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
10.Math.max()
返回值:函数返回作为输入参数的最大数字,如果没有参数,则返回-Infinity
Math.max()
Math.max(value0)
Math.max(value0, value1)
Math.max(value0, value1, /* … ,*/ valueN)
value1, value2, … , valueN
0个或多个数字,将在其中选择并返回最大的值;
console.log(Math.max(1, 3, 2));
// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// expected output: 3
11.Math.min()
**返回值:**返回作为输入参数的数字中最小的那一个,如没有参数则返回Infinity
console.log(Math.min(2, 3, 1));
// expected output: 1
console.log(Math.min(-2, -3, -1));
// expected output: -3
const array1 = [2, 3, 1];
console.log(Math.min(...array1));
// expected output: 1
Math.min()
Math.min(value0)
Math.min(value0, value1)
Math.min(value0, value1, /* … ,*/ valueN)
value1, …, valueN
0 个或多个数字,将在其中选择,并返回最小值。
12.Math.pow()
Math.pow()函数返回基数(base)的指数(exponent)次幂,即 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
13.Math.random()
随机数很常用了吧
Math.random() 函数返回一个浮点数,伪随机数在范围0到小于1,也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围.实现将初始种子选择到随机数生成算法;它不能被用户选择或重置;
13.1:得到1 > a > 0的随机数
function getRandom() {
return Math.random();
}
13.2:得到一个两数之间的随机数
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
return Math.random() * (10 - 1) + 1;
//expected output : 1 ~ 10 随机数,不是整数
}
13.3得到一个两数之间的随机整数
function getRandomArbitrary(min,max){
return Math.floor(Math.random() * (max - min) + min);
}
13.4得到两个数之间的随机整数,包括两个数;
function getRandomIntInclusive(min,max){
return Math.floor(Math.random() * (max - min + 1) - min);
}
14.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