JavaScript中常用的数学方法

一:说出常用的数学方法!

  1. Math.max(x,y,z) 获取最大值
  2. Math.min() 获取最小值
  3. Math.abs() 绝对值
  4. Math.ceil() 向上取整
  5. Math.floor() 向下取整
  6. Math.round() 四舍五入
  7. Math.random() 随机数
  8. Math.sin() 正弦函数
  9. Math.cos() 余弦函数
  10. Math.sqrt(num) num开平方
  11. Math.pow(x,y) x的y次方

    Math.max(x,y,z) 获取最大值

enter image description here

Math.min() 获取最小值

enter image description here

Math.abs() 绝对值

enter image description here

Math.ceil() 向上取整

enter image description here

Math.floor() 向下取整

enter image description here

Math.round() 四舍五入

enter image description here

Math.random() 随机数

enter image description here

随机数有必要在这里特别说明一下 Math.random()取得的随机数区间是 [0,1) 即: 0<=num<1

通常我们用到的不是[0,1)这个区间的,还有我们获取的一般是整数!

比如1~100之间的随机整数

Math.random()*100 获取到的随机数是 [0,100) (含小数)

Math.floor(Math.random()*100) 获取到的随机数是 [0,99] 整数

Math.floor(Math.random()*100)+1 获取到的随机数是 [1,100] 整数

再比如获取10~50之间的随机数

Math.random()*(50-10) 获取到的随机数是[0,40) (含小数)

Math.floor(Math.random()*(50-10+1)) [0,40]

Math.floor(Math.random()*(50-10+1))+10 [10, 50]

那么我想获取[m,n]这个区间的随机整数怎么获取呢?

Math.floor(Math.random()*(n-m+1))+m) [m,n] 闭合区间的随机整数

Math.sin() 正弦函数

enter image description here

Math.cos() 余弦函数

enter image description here

Math.sqrt(num) num开平方

enter image description here

Math.pow(x,y) x的y次方

enter image description here

posted @ 2015-01-06 14:31  huhl  阅读(353)  评论(0)    收藏  举报