JavaScript笔记(二)

  |   Javascript 前端

3.1.3 Arithmetic in JavaScript

In addition to these basic arithmetic operators, JavaScript supports more complex mathematical operations through a set of functions and constants defined as properties of the Math object:

Math.pow(2,53)           // => 9007199254740992: 2 to the power 53
Math.round(.6)           // => 1.0: round to the nearest integer
Math.ceil(.6)            // => 1.0: round up to an integer
Math.floor(.6)           // => 0.0: round down to an integer
Math.abs(-5)             // => 5: absolute value
Math.max(x,y,z)          // Return the largest argument
Math.min(x,y,z)          // Return the smallest argument
Math.random()            // Pseudo-random number x where 0 <= x < 1.0
Math.PI                  // π: circumference of a circle / diameter
Math.E                   // e: The base of the natural logarithm
Math.sqrt(3)             // The square root of 3
Math.pow(3, 1/3)         // The cube root of 3
Math.sin(0)              // Trigonometry: also Math.cos, Math.atan, etc.
Math.log(10)             // Natural logarithm of 10
Math.log(100)/Math.LN10  // Base 10 logarithm of 100
Math.log(512)/Math.LN2   // Base 2 logarithm of 512
Math.exp(3)              // Math.E cubed

3.1.3 JavaScript中的算术运算

除了基本的运算符外,JavaScript还支持更加复杂的算木运算。这些复杂运算通过作为Math对象的属性定义的函数和常量来实现。

Math.pow(2,53)           // => 9007199254740992: 2的53次幂
Math.round(.6)           // => 1.0: 四舍五入
Math.ceil(.6)            // => 1.0: 向上求整
Math.floor(.6)           // => 0.0: 向下求整
Math.abs(-5)             // => 5: 求绝对值
Math.max(x,y,z)          // 返回最大值
Math.min(x,y,z)          // 返回最小值
Math.random()            // 生成一个大于等于0小于1.0的伪随机数
Math.PI                  // π: 圆周率
Math.E                   // e: 自然对数的底数
Math.sqrt(3)             // 3的平方根
Math.pow(3, 1/3)         // 3的立方根
Math.sin(0)              // 三角函数:还有Math.cos,Math.atan等
Math.log(10)             // 10的自然对数
Math.log(100)/Math.LN10  // 以10为底100的对数
Math.log(512)/Math.LN2   // 以2为底512的对数
Math.exp(3)              // e的三次幂

来源:《JavaScript权威指南》--3.1.3 JavaScript中的算术运算 35页-36页