# Summary of Math built-in object knowledge in JS

### 5.4.2 Math

The   Math object serves as a place to store mathematical formulas, information, and calculations. Some properties and methods of auxiliary calculation are provided.

note: Math objects provide much faster calculations than those implemented directly in JavaScript, because math objects use more efficient implementations and processor instructions in the JavaScript engine. However, the problem with math calculation is that the accuracy will vary depending on the browser, operating system, instruction set and hardware.

#### 01. Math object properties

The attributes of   Math objects are used to hold some special values in mathematics.

attributeexplain
Math.EValue of base e of natural logarithm
Math.LN10Base 10 natural logarithm
Math.LN2Natural logarithm with base 2
Math.LOG2ELogarithm of base e of 2
Math.LOG10ELogarithm of base e of 10
Math.PIValue of π
Math.SQRT1_2Square root of 1 / 2
Math.SQRT2Square root of 2

Math.PI is often used to calculate the area of a circle, and others are not used very often.

#### 02. min() and max() methods

The   min() and max() methods are used to determine the minimum and maximum values in a set of values. Both methods accept any number of parameters:

```let max = Math.max(Math.PI,0,2,1,89);
console.log(max);	// 89
let min = Math.min(3,5,4,12,Math.PI,0,-1);
console.log(min);	// -1
```

to know the maximum or minimum value in the array, you only need to use the extension operator of the array:

```let values = [1,2,3,4,5,6,7,8,9];
console.log(Math.max(...values));	// 9
console.log(Math.min(...values));	// 1
```

#### 03. Rounding method

next are four methods for rounding small values to integers:

• Math.ceil(): always round up to the nearest integer.

• Math.floor(): always round down to the nearest integer.

• Math.round(): perform rounding.

• Math. Forward(): returns the single precision (32-bit) floating-point value representation with the closest value.

```console.log(Math.ceil(25.9));		// 26
console.log(Math.ceil(25.5));		// 26
console.log(Math.ceil(25.1));		// 26
console.log(Math.floor(25.9));  	// 25
console.log(Math.floor(25.5));  	// 25
console.log(Math.floor(25.1));  	// 25
console.log(Math.round(25.9));  	// 26
console.log(Math.round(25.5));  	// 26
console.log(Math.round(25.1));  	// 25
console.log(Math.fround(0.4));  	// 0.4000000059604645
console.log(Math.fround(0.5));  	// 0.5
console.log(Math.fround(25.9)); 	// 25.899999618530273
```

#### 04. random() method

the Math.random() method returns a random number in the range of 0 ~ 1, including 0 but not 1.

select a number randomly from a group of integers: first indicates what number to start with, and total indicates how many numbers there are in total.

```let number = Math.floor(Math,random()*total + first);
```

if a number is randomly selected from the range of 1 ~ 10:

```let number = Math.floor(Math.random()*10 + 1);
```

if a number is randomly selected from the range of 2 ~ 10:

```let number = Math.floor(Math.random()*9 + 2);
```

you can use the next function to obtain the number from start to end:

```function randomNumber(start,end){
let total = end - start + 1;
return Math.floor(Math.random()*total + start);
}
let num = randomNumber(1,2);
console.log(num);
```

using this function, it is easy to randomly select an element from an array, such as:

```let colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
let color = colors[randomNumber(0,colors.length-1)];
console.log(color);
```

#### 05. Other methods

Math objects also have many methods involving various simple or higher-order operations.

methodexplain
Math.abs(x)Returns the absolute value of x
Math.exp(x)Returns the x-Power of Math.E
Math.expm1(x)Equal to Math.exp(x) - 1
Math.log(x)Returns the natural logarithm of x
Math.log1p(x)Equal to 1+ Math.log(x)
Math.pow(x,power)Returns the power of x
Math.hypot(...nums)Returns the square root of the sum of squares of each number in nums
Math.clz32(x)Returns the number of leading zeros of a 32-bit integer x
Math.sign(x)Returns 1, 0, - 0, or - 1 representing the x symbol
Math.trunc(x)Returns the integer part of x, removing all decimals
Math.sqrt(x)Returns the square of x
Math.cbrt(x)Returns the cube of x
Math.acos(x)Returns the arccosine of x
Math.acosh(x)Returns the hyperbolic cosine of x
Math.asin(x)Returns the inverse sine of x
Math.asinh(x)Returns the hyperbolic sine of x
Math.atan(x)Returns the arctangent of x
Math.atanh(x)Returns the hyperbolic tangent of x
Math.atan2(y,x)Returns the arctangent of y/x
Math.cos(x)Returns the cosine of x
Math.sin(x)Returns the sine of x
Math.tan(x)Returns the tangent of x

Posted on Fri, 22 Oct 2021 03:46:23 -0400 by Oren