【JavaScript】标准库:Math对象
文章目录
- 1 静态属性
- 2 静态方法
- 2.1 Math.abs()
- 2.2 Math.max(),Math.min()
- 2.3 Math.floor(),Math.ceil()
- 2.4 Math.round()
- 2.5 Math.pow()
- 2.6 Math.sqrt()
- 2.7 Math.log()
- 2.8 Math.exp()
- 2.9 Math.random()
- 2.10 三角函数方法
1 静态属性
Math对象的静态属性,提供以下一些数学常数。这些属性都是只读的,不能修改。
- Math.E:常数e。
- Math.LN2:2 的自然对数。
- Math.LN10:10 的自然对数。
- Math.LOG2E:以 2 为底的e的对数。
- Math.LOG10E:以 10 为底的e的对数。
- Math.PI:常数π。
- Math.SQRT1_2:0.5 的平方根。
- Math.SQRT2:2 的平方根。
2 静态方法
Math对象提供以下一些静态方法。
- Math.abs():绝对值
- Math.ceil():向上取整
- Math.floor():向下取整
- Math.max():最大值
- Math.min():最小值
- Math.pow():幂运算
- Math.sqrt():平方根
- Math.log():自然对数
- Math.exp():e的指数
- Math.round():四舍五入
- Math.random():随机数
2.1 Math.abs()
Math.abs方法返回参数值的绝对值。
Math.abs(1) // 1
Math.abs(-1) // 1
2.2 Math.max(),Math.min()
Math.max方法返回参数之中最大的那个值,Math.min返回最小的那个值。如果参数为空, Math.min返回Infinity, Math.max返回-Infinity。
Math.max(2, -1, 5) // 5
Math.min(2, -1, 5) // -1
Math.min() // Infinity
Math.max() // -Infinity
2.3 Math.floor(),Math.ceil()
Math.floor方法返回小于或等于参数值的最大整数(地板值)。
Math.floor(3.2) // 3
Math.floor(-3.2) // -4
Math.ceil方法返回大于或等于参数值的最小整数(天花板值)。
Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3
2.4 Math.round()
Math.round方法用于四舍五入。
Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1
// 等同于
Math.floor(x + 0.5)
注意,它对负数的处理(主要是对0.5的处理)。
Math.round(-1.1) // -1
Math.round(-1.5) // -1
Math.round(-1.6) // -2
2.5 Math.pow()
Math.pow方法返回以第一个参数为底数、第二个参数为指数的幂运算值。
// 等同于 2 ** 2
Math.pow(2, 2) // 4
// 等同于 2 ** 3
Math.pow(2, 3) // 8
2.6 Math.sqrt()
Math.sqrt方法返回参数值的平方根。如果参数是一个负值,则返回NaN。
Math.sqrt(4) // 2
Math.sqrt(-4) // NaN
2.7 Math.log()
Math.log方法返回以e为底的自然对数值。
Math.log(Math.E) // 1
Math.log(10) // 2.302585092994046
如果要计算以10为底的对数,可以先用Math.log求出自然对数,然后除以Math.LN10;求以2为底的对数,可以除以Math.LN2。
Math.log(100)/Math.LN10 // 2
Math.log(8)/Math.LN2 // 3
2.8 Math.exp()
Math.exp方法返回常数e的参数次方。
Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668
2.9 Math.random()
Math.random()返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。
任意范围的随机数生成函数如下。
function getRandomArbitrary(min, max) {return Math.random() * (max - min) + min;
}getRandomArbitrary(1.5, 6.5)
任意范围的随机整数生成函数如下。
function getRandomInt(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min;
}getRandomInt(1, 6)
返回随机字符的例子如下。
function random_str(length) {var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';ALPHABET += 'abcdefghijklmnopqrstuvwxyz';ALPHABET += '0123456789-_';var str = '';for (var i = 0; i < length; ++i) {var rand = Math.floor(Math.random() * ALPHABET.length);str += ALPHABET.substring(rand, rand + 1);}return str;
}random_str(6) // "NdQKOr"
2.10 三角函数方法
Math对象还提供一系列三角函数方法。
- Math.sin():返回参数的正弦(参数为弧度值)
- Math.cos():返回参数的余弦(参数为弧度值)
- Math.tan():返回参数的正切(参数为弧度值)
- Math.asin():返回参数的反正弦(返回值为弧度值)
- Math.acos():返回参数的反余弦(返回值为弧度值)
- Math.atan():返回参数的反正切(返回值为弧度值)