JSmath

作者: 董二干先生 | 来源:发表于2019-05-17 15:38 被阅读0次

Math对象是JavaScript的内置对象,提供一系列数学常数和数学方法。Math对象只提供了静态的属性和方法,所以使用时不用实例化。

round

用于四舍五入。

Math.round(0.1) // 0
Math.round(0.5) // 1

abs

绝对值。

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

max/min

max方法返回最大的参数,min方法返回最小的参数。

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

floor/ceil

floor方法返回小于参数值的最大整数。

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

ceil方法返回大于参数值的最小整数。

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

pow,sqrt

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

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

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

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

log,exp

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

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

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

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

random

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

Math.random() // 0.7151307314634323

返回给定范围的随机数。

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

三角函数

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

相关文章

  • JSmath

    Math对象是JavaScript的内置对象,提供一系列数学常数和数学方法。Math对象只提供了静态的属性和方法,...

  • JSMath数组Date

    Math 1、写一个函数,返回从min到max之间的随机整数,包括min不包括max 2、写一个函数,返回从min...

网友评论

      本文标题:JSmath

      本文链接:https://www.haomeiwen.com/subject/vlcoaqtx.html