Math 对象dom
封装了数学相关的 属性和方法。函数
和其余对象不同,Math 不是一个构造函数,因此不能 new 生成实例,spa
其全部属性和方法都必须在 Math 对象上调用。code
Math.PI // 圆周率 3.1415926 Math.E // 算数常量 e ,约为 2.718 Math.LN2 // 以 e 为底,2 的对数,约为 0.693 Math.LN10 // 以 e 为底,10 的对数,约为 2.302 Math.LOG2E // 以 2 为底,e 的对数,约为 1.414 Math.LOG10E // 以 10 为底,e 的对数,约为 0.434
Math.abs(x) // x 的绝对值 Math.log(x) // log 以 e 为底,x 的对数 Math.max(x,y) // 返回最大值 能够比较多个值 若是参数为空,返回 infinity Math.min(x,y) // 返回最小值 能够比较多个值 若是参数为空,返回 -infinity Math.pow(x,y) // x 的 y 次方 Math.random() // 产生一个 0-1 直接的随机数 , 不包含 0,1 Math.round(x) // 四舍五入 x Math.sqrt(x) // 平方根 根号x 若是参数是一个负值,则返回 Math.toSource() // 返回 Math 对象的源代码 Math.valueof() // 返回 Math 对象的原始值
NaN
Math.ceil(x) // 向上取整 x
Math.floor(x) // 向下取整 x
// Math.sin():返回参数的正弦(参数为弧度值)
// Math.cos():返回参数的余弦(参数为弧度值)
// Math.tan():返回参数的正切(参数为弧度值)
// Math.asin():返回参数的反正弦(返回值为弧度值)
// Math.acos():返回参数的反余弦(返回值为弧度值)
// Math.atan():返回参数的反正切(返回值为弧度值)对象
console.log( Math.round(Math.random()*10) );
console.log( Math.round(Math.random()*9)+1 );
function myRandom(x,y){ return Math.round(Math.random()*(y-x)+x); }