1.不常用的相关数学常量、三角函数
Math.E // e的值:2.718281828459045
Math.PI // PI的值:3.141592653589793
var a=Math.exp(-1); // 0.36787944117144233
var b=Math.exp(5); // 148.4131591025766
var c=Math.exp(10); // 22026.465794806718
Math.sin(3) // 返回一个数字的正弦值:0.1411200080598672
Math.cos(3) // 返回一个数字的余弦值:-0.9899924966004454
Math.tan(90); // 返回一个表示某个角的正切的数字:-1.995200412208242
Math.log(2); // 返回一个数的自然对数(基于E):0.6931471805599453
2.Math.abs方法返回参数值的绝对值。
console.log(Math.abs(-12));
3.Math.max方法返回参数之中最大值、Math.min返回最小个值。
如果参数为空, Math.min返回Infinity, Math.max返回-Infinity。
console.log(Math.min(2, 5, -1)); // -1
console.log(Math.min()); // Infinity
console.log(Math.max(2, 5, -1)); // 5
console.log(Math.max()); // -Infinity
4.Math.floor方法对一个数进行下舍入、Math.ceil方法对一个数进行上舍入。
console.log(Math.floor(3.14)); // 3
console.log(Math.ceil(3.14)); // 4
5.Math.round方法用于四舍五入。
var a=Math.round(2.60); // 3
var b=Math.round(2.50); // 3
var c=Math.round(2.49); // 2
var d=Math.round(-2.60); // -3
var e=Math.round(-2.50); // -2
var f=Math.round(-2.49); // -2
6.Math.pow(次方运算)返回 x 的 y 次幂。
语法:Math.pow(x,y)
var a=Math.pow(0,1); // 0
var b=Math.pow(1,1); // 1
var c=Math.pow(1,10); // 1
var d=Math.pow(3,3); // 27
var e=Math.pow(-3,3); // -27
var f=Math.pow(2,4); // 16
7.Math.sqrt方法返回参数值的平方根。
如果参数是一个负值,则返回NaN。
var a=Math.sqrt(0); // 0
var b=Math.sqrt(1); // 1
var c=Math.sqrt(9); // 3
var d=Math.sqrt(64); // 8
var e=Math.sqrt(-9); // NaN
8.Math.random() (用于随机取数)
返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。
function getRandomInt(min, max) {
return parseInt(Math.random() * (max - min + 1)) + min;
} //10-100
Math.random()*91; // [0,91)
parseInt(Math.random()*91); // [0,90]
console.log(parseInt(Math.random()*91) + 10); // [10,100]