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()
    • [0,1)之间的随机数

      Math 属性

      Math.E // 返回欧拉指数(Euler’s number)
      Math.PI // 返回圆周率(PI)
      Math.SQRT2 // 返回 2 的平方根
      Math.SQRT1_2 // 返回 1/2 的平方根
      Math.LN2 // 返回 2 的自然对数
      Math.LN10 // 返回 10 的自然对数
      Math.LOG2E // 返回以 2 为底的 e 的对数(约等于 1.414)
      Math.LOG10E // 返回以 10 为底的 e 的对数(约等于0.434)

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()
    • 一起使用返回随机整数
  1. Math.floor(Math.random() * 10); // 返回 0 至 9 之间的数
  2. Math.floor(Math.random() * 11); // 返回 0 至 10 之间的数
  3. Math.floor(Math.random() * 100); // 返回 0 至 99 之间的数
  4. Math.floor(Math.random() * 101); // 返回 0 至 100 之间的数
  5. 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; }

```

逻辑

boolean()

  • true
  • false

    • ±0
    • 空字符串
    • undefined
    • null
    • false
    • NaN

      比较运算符

  • ==

  • ===
    • 值相等并且类型相等
  • !=
    • 不相等
  • !==
    • 值不相等或者类型不相等
  • >
  • =

  • <
  • <=

    逻辑运算符

  • &&

  • ||

image.png