buy the book to support the author.

Chapter 21. Math

The Math object is used as a namespace for several math functions. This chapter provides an overview.

Math Properties

The properties of Math are as follows:

  • Math.E
  • Euler’s constant (e)
  • Math.LN2
  • Natural logarithm of 2
  • Math.LN10
  • Natural logarithm of 10
  • Math.LOG2E
  • Base 2 logarithm of e
  • Math.LOG10E
  • Base 10 logarithm of e
  • Math.PI
  • The ratio of the circumference of a circle to its diameter (3.14159 …), π
  • Math.SQRT1_2
  • The square root of one-half, 21. Math - 图1
  • Math.SQRT2
  • The square root of two, 21. Math - 图2

Numerical Functions

The numerical functions of Math include the following:

  • Math.abs(x)
  • Returns the absolute value of x.
  • Math.ceil(x)
  • Returns the smallest integer ≥ x:
  1. > Math.ceil(3.999)
  2. 4
  3. > Math.ceil(3.001)
  4. 4
  5. > Math.ceil(-3.001)
  6. -3
  7. > Math.ceil(3.000)
  8. 3

For more on converting floating-point numbers to integers, see Converting to Integer.

  • Math.exp(x)
  • Returns ex where e is Euler’s constant (Math.E). This is the inverse of Math.log().
  • Math.floor(x)
  • Returns the largest integer ≤ x:
  1. > Math.floor(3.999)
  2. 3
  3. > Math.floor(3.001)
  4. 3
  5. > Math.floor(-3.001)
  6. -4
  7. > Math.floor(3.000)
  8. 3

For more on converting floating-point numbers to integers, see Converting to Integer.

  • Math.log(x)
  • Returns the natural (base is Euler’s constant) logarithm ln(x) of x. This is the inverse of Math.exp().
  • Math.pow(x, y)
  • Returns xy, x raised to the power of y:
  1. > Math.pow(9, 2)
  2. 81
  3. > Math.pow(36, 0.5)
  4. 6
  • Math.round(x)
  • Returns x rounded to the nearest integer (the greater one if it is between two integers):
  1. > Math.round(3.999)
  2. 4
  3. > Math.round(3.001)
  4. 3
  5. > Math.round(3.5)
  6. 4
  7. > Math.round(-3.5)
  8. -3

For more on converting floating-point numbers to integers, see Converting to Integer.

  • Math.sqrt(x)
  • Returns 21. Math - 图3, the square root of x:
  1. > Math.sqrt(256)
  2. 16

Trigonometric Functions

The trigonometric methods accept and return angles as radians. The following functions show you how you could implement conversions, should you need to:

  • From degrees to radians:
  1. function toRadians(degrees) {
  2. return degrees / 180 * Math.PI;
  3. }

Here is the interaction:

  1. > toRadians(180)
  2. 3.141592653589793
  3. > toRadians(90)
  4. 1.5707963267948966
  • From radians to degrees:
  1. function toDegrees(radians) {
  2. return radians / Math.PI * 180;
  3. }

Here is the interaction:

  1. > toDegrees(Math.PI * 2)
  2. 360
  3. > toDegrees(Math.PI)
  4. 180

The trigonometric methods are as follows:

  • Math.acos(x)
  • Returns the arc cosine of x.
  • Math.asin(x)
  • Returns the arc sine of x.
  • Math.atan(x)
  • Returns the arc tangent of x.
  • Math.atan2(y, x)
  • Returns the arc tangent of the quotient 21. Math - 图4.
  • Math.cos(x)
  • Returns the cosine of x.
  • Math.sin(x)
  • Returns the sine of x.
  • Math.tan(x)
  • Returns the tangent of x.

Other Functions

Following are the remaining Math functions:

  • Math.min(x1?, x2?, …)
  • Returns the smallest number among the parameters:
  1. > Math.min()
  2. Infinity
  3. > Math.min(27)
  4. 27
  5. > Math.min(27, -38)
  6. -38
  7. > Math.min(27, -38, -43)
  8. -43

Use it on arrays via apply() (see func.apply(thisValue, argArray)):

  1. > Math.min.apply(null, [27, -38, -43])
  2. -43
  • Math.max(x1?, x2?, …)
  • Returns the largest number among the parameters:
  1. > Math.max()
  2. -Infinity
  3. > Math.max(7)
  4. 7
  5. > Math.max(7, 10)
  6. 10
  7. > Math.max(7, 10, -333)
  8. 10

Use it on arrays via apply() (see func.apply(thisValue, argArray)):

  1. > Math.max.apply(null, [7, 10, -333])
  2. 10
  • Math.random()
  • Returns a pseudorandom number r, 0 ≤ r < 1.The following function uses Math.random() to compute a random integer:
  1. /**
  2. * Compute a random integer within the given range.
  3. *
  4. * @param [lower] Optional lower bound. Default: zero.
  5. * @returns A random integer i, lower ≤ i < upper
  6. */
  7. function getRandomInteger(lower, upper) {
  8. if (arguments.length === 1) {
  9. upper = lower;
  10. lower = 0;
  11. }
  12. return Math.floor(Math.random() * (upper - lower)) + lower;
  13. }