Math 对象

    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 的平方根。

    这些属性都是只读的,不能修改。

    静态方法

    Math对象提供以下一些静态方法。

    • Math.abs():绝对值
    • Math.ceil():向上取整
    • Math.floor():向下取整
    • Math.max():最大值
    • Math.min():最小值
    • Math.pow():幂运算
    • Math.sqrt():平方根
    • Math.log():自然对数
    • Math.exp()e的指数
    • Math.round():四舍五入
    • Math.random():随机数

    Math.abs方法返回参数值的绝对值。

    1. Math.abs(1) // 1

    Math.max(),Math.min()

    Math.max方法返回参数之中最大的那个值,Math.min返回最小的那个值。如果参数为空, Math.min返回Infinity, Math.max返回-Infinity

    1. Math.max(2, -1, 5) // 5
    2. Math.min(2, -1, 5) // -1
    3. Math.min() // Infinity

    Math.floor(),Math.ceil()

    Math.floor方法返回小于参数值的最大整数(地板值)。

    1. Math.floor(3.2) // 3
    2. Math.floor(-3.2) // -4

    Math.ceil方法返回大于参数值的最小整数(天花板值)。

    1. Math.ceil(3.2) // 4
    2. Math.ceil(-3.2) // -3
    1. function ToInteger(x) {
    2. x = Number(x);
    3. return x < 0 ? Math.ceil(x) : Math.floor(x);
    4. }
    5. ToInteger(3.2) // 3
    6. ToInteger(3.5) // 3
    7. ToInteger(3.8) // 3
    8. ToInteger(-3.2) // -3
    9. ToInteger(-3.5) // -3
    10. ToInteger(-3.8) // -3

    上面代码中,不管正数或负数,ToInteger函数总是返回一个数值的整数部分。

    Math.round方法用于四舍五入。

    注意,它对负数的处理(主要是对0.5的处理)。

    1. Math.round(-1.1) // -1
    2. Math.round(-1.5) // -1
    3. Math.round(-1.6) // -2

    Math.pow()

    Math.pow方法返回以第一个参数为底数、第二个参数为指数的幂运算值。

    1. // 等同于 2 ** 2
    2. Math.pow(2, 2) // 4
    3. // 等同于 2 ** 3
    4. Math.pow(2, 3) // 8

    下面是计算圆面积的方法。

    1. var radius = 20;
    2. var area = Math.PI * Math.pow(radius, 2);

    Math.sqrt()

    Math.sqrt方法返回参数值的平方根。如果参数是一个负值,则返回NaN

    1. Math.sqrt(4) // 2
    2. Math.sqrt(-4) // NaN

    Math.log方法返回以e为底的自然对数值。

    1. Math.log(Math.E) // 1
    2. Math.log(10) // 2.302585092994046

    Math.exp()

    Math.exp方法返回常数e的参数次方。

    1. Math.exp(3) // 20.085536923187668

    Math.random()

    Math.random()返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。

    1. Math.random() // 0.7151307314634323

    任意范围的随机数生成函数如下。

    1. function getRandomArbitrary(min, max) {
    2. return Math.random() * (max - min) + min;
    3. }
    4. getRandomArbitrary(1.5, 6.5)
    5. // 2.4942810038223864

    任意范围的随机整数生成函数如下。

    1. function getRandomInt(min, max) {
    2. return Math.floor(Math.random() * (max - min + 1)) + min;
    3. }
    4. getRandomInt(1, 6) // 5

    返回随机字符的例子如下。

    1. function random_str(length) {
    2. var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    3. ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
    4. ALPHABET += '0123456789-_';
    5. var str = '';
    6. for (var i = 0; i < length; ++i) {
    7. var rand = Math.floor(Math.random() * ALPHABET.length);
    8. str += ALPHABET.substring(rand, rand + 1);
    9. }
    10. return str;
    11. }
    12. random_str(6) // "NdQKOr"

    上面代码中,random_str函数接受一个整数作为参数,返回变量ALPHABET内的随机字符所组成的指定长度的字符串。

    Math对象还提供一系列三角函数方法。

    • Math.sin():返回参数的正弦(参数为弧度值)
    • Math.cos():返回参数的余弦(参数为弧度值)
    • Math.tan():返回参数的正切(参数为弧度值)
    • Math.asin():返回参数的反正弦(返回值为弧度值)
    • Math.acos():返回参数的反余弦(返回值为弧度值)
    • :返回参数的反正切(返回值为弧度值)