JavaScript中常用的数学方法
一:说出常用的数学方法!
- Math.max(x,y,z) 获取最大值
- Math.min() 获取最小值
- Math.abs() 绝对值
- Math.ceil() 向上取整
- Math.floor() 向下取整
- Math.round() 四舍五入
- Math.random() 随机数
- Math.sin() 正弦函数
- Math.cos() 余弦函数
- Math.sqrt(num) num开平方
-
Math.pow(x,y) x的y次方
Math.max(x,y,z) 获取最大值

Math.min() 获取最小值

Math.abs() 绝对值

Math.ceil() 向上取整

Math.floor() 向下取整

Math.round() 四舍五入

Math.random() 随机数

随机数有必要在这里特别说明一下 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() 正弦函数

Math.cos() 余弦函数

Math.sqrt(num) num开平方

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


浙公网安备 33010602011771号