math
math 对象
- math.PI;
- Math.round
- 四舍五入
- Math.pow(x,y)
- x 的 y 次幂
- Math.sqrt(x)
- x平方根
- Math.abs()
- 绝对值
- math.ceil()
- x 向上最接近的整数
- Math.floor()
- x 向下最接近的整数
- Math.sin()
- 正弦
- Math.cos()
- 余弦
- math.max()
- math.min()
- Math.random()
math 对象方法
abs(x) | 返回 x 的绝对值 |
---|---|
acos(x) | 返回 x 的反余弦值,以弧度计 |
asin(x) | 返回 x 的反正弦值,以弧度计 |
atan(x) | 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。 |
atan2(y,x) | 返回从 x 轴到点 (x,y) 的角度 |
ceil(x) | 对 x 进行上舍入 |
cos(x) | 返回 x 的余弦 |
exp(x) | 返回 Ex 的值 |
floor(x) | 对 x 进行下舍入 |
log(x) | 返回 x 的自然对数(底为e) |
max(x,y,z,…,n) | 返回最高值 |
min(x,y,z,…,n) | 返回最低值 |
pow(x,y) | 返回 x 的 y 次幂 |
random() | 返回 0 ~ 1 之间的随机数 |
round(x) | 把 x 四舍五入为最接近的整数 |
sin(x) | 返回 x(x 以角度计)的正弦 |
sqrt(x) | 返回 x 的平方根 |
tan(x) | 返回角的正切 |
随机
- Math.random()
- [0,1)之间的随机数
- Math.random()与 Math.floor()
- 一起使用返回随机整数
Math.floor(Math.random() * 10); // 返回 0 至 9 之间的数
Math.floor(Math.random() * 11); // 返回 0 至 10 之间的数
Math.floor(Math.random() * 100); // 返回 0 至 99 之间的数
Math.floor(Math.random() * 101); // 返回 0 至 100 之间的数
Math.floor(Math.random() * 100) + 1; // 返回 1 至 100 之间的数
- Math.max()和 Math.min()
- 一起使用返回一个介于 [min,max)之间的随机数 ```javascript function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; }
```