Math

Math Object

Math is a native object of JavaScript, which provides various mathematical functions. The object is not a constructor and cannot generate an instance. All properties and methods must be called on the Math object.

Static properties The static properties of the

Math object provide the following mathematical constants.

-Math.E: Constant e. -Math.LN2: the natural logarithm of 2. -Math.LN10: The natural logarithm of 10. -Math.LOG2E: base 2 logarithm of e. -Math.LOG10E: base 10 logarithm of e. -Math.PI: The constant π. -Math.SQRT1_2: the square root of 0.5. -Math.SQRT2: the square root of 2.

Math.E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 / /1.4142135623730951

These attributes are read-only and cannot be modified.

Static methods The

Math object provides the following static methods.

-Math.abs(): absolute value -Math.ceil(): round up-Math.floor(): round down -Math.max(): maximum value -Math .min(): minimum value -Math.pow(): power operation -Math.sqrt(): square root -Math.log(): natural logarithm -Math.exp(): The exponent of e-Math.round (): rounding

`Math.random()

: random number ### Math.abs() TheMath.abs` method returns the absolute value of the parameter value.

Math.abs(1); // 1
Math.abs(-1); // 1

Math.max(), Math.min()

Math.max method returns among the parameters The largest value, Math.min returns the smallest value. If the parameter is empty, Math.min returns Infinity, and Math.max returns -Infinity.

Math.max(2, -1, 5); // 5
Math.min(2, -1, 5); // -1
Math.min(); // Infinity
Math.max(); // -Infinity

Math.floor(), Math.ceil()

The Math.floor method returns the largest integer (floor value) that is less than or equal to the parameter value.

Math.floor(3.2); // 3
Math.floor(-3.2); // -4

Math.ceil method returns the smallest integer (ceiling value) greater than or equal to the parameter value.

Math.ceil(3.2); // 4
Math.ceil(-3.2); // -3

These two methods can be combined to implement a function that always returns the integer part of the value.

function ToInteger(x) {
  x = Number(x);
  return x <0? Math.ceil(x): Math.floor(x);
}

ToInteger(3.2) // 3
ToInteger(3.5) // 3
ToInteger(3.8) // 3
ToInteger(-3.2) // -3
ToInteger(-3.5) // -3
ToInteger(-3.8) // -3
```In the

above code, regardless of a positive or negative number, the `ToInteger` function always returns the integer part of a value.

### Math.round() The

`Math.round` method is used for rounding.

```javascript
Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1

// equivalent to
Math.floor(x + 0.5)

Note that it is correct Handling of negative numbers (mainly handling of 0.5).

Math.round(-1.1); // -1
Math.round(-1.5); // -1
Math.round(-1.6); // -2

Math.pow()

Math The .pow method returns a power operation value with the first parameter as the base and the second parameter as the exponent.

// equivalent to 2 ** 2
Math.pow(2, 2); // 4
// equivalent to 2 ** 3
Math.pow(2, 3); // 8

Here is how to calculate the area of ​​a circle.

var radius = 20;
var area = Math.PI * Math.pow(radius, 2);

Math.sqrt() The

Math.sqrt method returns the square root of the parameter value. If the argument is a negative value, NaN is returned.

Math.sqrt(4); // 2
Math.sqrt(-4); // NaN

Math.log() The

Math.log method returns the natural pair with e as the base Numerical value.

Math.log(Math.E); // 1
Math.log(10); // 2.302585092994046

If you want to calculate the base 10 logarithm, you can first use Math.log to find the natural pair Number, and then divide by Math.LN10; to find the logarithm to base 2, you can divide by Math.LN2.

Math.log(100) / Math.LN10; // 2
Math.log(8) / Math.LN2; // 3

Math.exp()

Math.exp method returns a constant e to the power of the parameter.

Math.exp(1); // 2.718281828459045
Math.exp(3); // 20.085536923187668

Math.random()

Math.random() returns a pseudo-random number between 0 and 1, which may be equal to 0, but must be less than 1.

Math.random(); // 0.7151307314634323

The random number generation function of any range is as follows.

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

getRandomArbitrary(1.5, 6.5);
// 2.4942810038223864

The random integer generation function of any range is as follows.

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

getRandomInt(1, 6); // 5

Return random characters The example is as follows.

function random_str(length) {
  var ALPHABET ='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  ALPHABET +='abcdefghijklmnopqrstuvwxyz';
  ALPHABET += '0123456789-_';
  var str ='';
  for (var i = 0; i <length; ++i) {
    var rand = Math.floor(Math.random() * ALPHABET.length);
    str += ALPHABET.substring(rand, rand + 1);
  }
  return str;
}

random_str(6) // "NdQKOr "
```In the

above code, the `random_str` function accepts an integer as a parameter and returns a string of specified length composed of random characters in the variable `ALPHABET`.

### Trigonometric function methods The

`Math` object also provides a series of trigonometric function methods.

-`Math.sin()`: returns the sine of the parameter (the parameter is a radian value)
-`Math.cos()`: returns the cosine of the parameter (the parameter is a radian value)
-`Math.tan()`: returns the sine of the parameter Tangent (parameter is radian value)
-`Math.asin()`: returns the arc sine of the parameter (the return value is a radian value)
-`Math.acos()`: returns the arc cosine of the parameter (the return value is a radian value)
-`Math.atan()` : Return the arc tangent of the parameter (return value is the radian value)

```javascript
Math.sin(0) // 0
Math.cos(0) // 1
Math.tan(0) // 0

Math.sin(Math. PI / 2) // 1

Math.asin(1) // 1.5707963267948966
Math.acos(1) // 0
Math.atan(1) // 0.7853981633974483