Math

背景

Math对象是JavaScript的内置对象,提供一系列数学常数和数学方法。该对象不是构造函数,所以不能生成实例,所有的属性和方法都必须在Math对象上调用

new Math()
// TypeError: object is not a function

属性

Math对象有一些只读的数学常数。

取值如下:

Math.E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 // 1.4142135623730951

方法

Math对象提供了一些数学方法。

Math.round()

Math.round()用作四舍五入。需要注意对负值的0.5部分的处理。

//正值部分
Math.round(0.1) // 0
Math.round(0.5) // 1
//负值部分
Math.round(-1.1) // -1
Math.round(-1.5) // -1

Math.abs(),Math.max(),Math.min()

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

Math.abs(1) // 1
Math.abs(-1) // 1

Math.max()方法返回参数的最大值。Math.min()方法返回最小的参数。

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

Math.floor(),Math.ceil()

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

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

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

Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3

Math.pow(),Math.sqrt()

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

Math.pow(2, 2) // 4
Math.pow(2, 3) // 8

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

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

Math.log(),Math.exp()

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

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

求以10为底的对数,可以除以Math.LN10;求以2为底的对数,可以除以Math.LN2

Math.log(100)/Math.LN10 // 2
Math.log(8)/Math.LN2 // 3

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

Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668

Math.random()

该方法返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。

Math.random() // 0.7151307314634323

// 返回给定范围内的随机数
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

// 返回给定范围内的随机整数
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

下面的代码中,random_base64可以在给定范围ALPHABET中返回一个随机字符。

var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';

random_base64 = function random_base64(length) {
  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;
}

三角函数

sin方法返回参数的正弦,cos方法返回参数的余弦,tan方法返回参数的正切。

Math.sin(0) // 0
Math.cos(0) // 1
Math.tan(0) // 0

asin方法返回参数的反正弦,acos方法返回参数的反余弦,atan方法返回参数的反正切。这个三个方法的返回值都是弧度值。

Math.asin(1) // 1.5707963267948966
Math.acos(1) // 0
Math.atan(1) // 0.7853981633974483

cosh方法返回参数的双曲余弦值,sinh方法返回参数的双曲正弦,tanh方法返回参数的双曲正切。如果x为NaN,则结果为NaN。 Math.sinh(1) // 1.1752011936438014 Math.cosh(1) // 1.5430806348152437 Math.tanh(1) // 0.7615941559557649

acosh方法返回参数的双曲反余弦值,asinh方法返回参数的反双曲正弦,atanh方法返回参数的反双曲正切。如果x为NaN,则结果为NaN。 Math.asinh(1) // 0.8813735870195429 Math.acosh(1) // 0 Math.atanh(1) // Infinity

atan2方法返回从X轴到(y,x)点的角度(以弧度为单位)。 Math.atan2(y, x)

扩展

Math.log2()

返回数字以 2 为底的对数。

Math.log2(x)  

如果 number 为 NaN,则结果将为 NaN。如果 x 小于零 (0),则此函数返回 NaN。

Math.log10()

返回数字以 10 为底的对数。

Math.log10(x) 

如果 number 为 NaN,则结果将为 NaN

Math.trunc()

返回数字的整数部分,删除任何小数数字。

Math.trunc(number)

所需 number 参数是一个数值表达式,它需要被截断的整数。如果 number 为 NaN,则结果将为 NaN

Math.sign()

返回数字符号,指示数字为正数、负数还是0。

Math.sign(number)

返回值为下列之一:

Math.cbrt()

返回数字的立方根。

Math.cbrt(8)  //2

如果 number 为 NaN,则结果将为 NaN

Math.fround()

返回最近的单精度浮点格式的数字。

Math.fround(1.56465465456465345646546454)  //1.5646547079086304

Math.hypot()

返回参数平方和的平方根。

Math.hypot(3, 4);
// Returns 5

Math.log1p()

返回 1 加上一个数字的的自然对数。

Math.log1p(x) 

如果 x 为 NaN,则此函数返回 NaN。如果 x 小于零 (0),则此函数返回 NaN。

Math.imul()

返回被视为 32 位带符号整数的两个数字的积。

var result1 = Math.imul(2, 5);
// result1 == 10

如果 number 为 0,则结果将为 32。如果 number 的 32 位二进制编码的最高有效位为 1,则结果将为 0。(估计这辈子都不会用这个方法

Math.clz32()

返回数字的 32 位二进制表示形式中的前导零位数。

Math.clz32(98989898989)  //4

如果 number 为 0,则结果将为 32。如果 number 的 32 位二进制编码的最高有效位为 1,则结果将为 0。(估计这辈子都不会用这个方法

Math.expm1()

返回 e(自然对数的底)的乘幂数减去 1 的结果。

Math.expm1(2)  //6.38905609893065

所需 number 参数是一个数值表达式,表示 e 的幂(这特么又是什么东西

mathjs