# A brief overview of Math

## Simple overview, Math

ECMAScript provides math objects as a place to save mathematical formulas, information and calculations. The Math object provides some properties and methods to assist in calculation.

``` 1.Math Object properties
Math Object has some properties, which are mainly used to save some special values in mathematics. The following properties
```
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

These meanings and usages are defined by ECMAScript specification and can be used when you need them

```2.min()and max()method
```

Math objects also provide many methods to assist in the execution of simple or complex mathematical calculations.
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, such as:

```let max = Math.max(3, 54 , 32, 16);
console.log(max); //54

let min = Math.min(3, 54, 32, 16);
console.log(min);  //3
```

In 3, 54, 32 and 16, Math.max() returns 54 and Math.min() returns 3. Using these two methods can avoid using additional loops and if statements to determine the maximum and minimum values of a set of values.
To know the maximum and minimum values in the array, you can use the extension operator as follows:

```let values = [1,2,3,4,5,6,7,8];
let max = Math.max(...val);
```
```3.Rounding method
```

Next, there are four methods for rounding small values to integers: Math.ceil(),Math.floor(),Math.round(),Math,froun(). These methods deal with rounding as follows:

• The Math.ceil() method always rounds up to the nearest integer.

• The Math.floor() method always rounds down to the nearest integer.

• The Math.round() method performs rounding.

• The Math,froun() method returns the closest single precision (32) floating-point value representation of the value.

• Here are the usage of these methods:

```console.log(Math.ceil(25.9));  //26
console.log(Math.ceil(25.5));  //26
console.log(Math.ceil(25.1));  //26

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

console.log(Math.floor(25.9));  //25
console.log(Math.floor(25.5));  //25
console.log(Math.floor(25.1));  //25
```

For all values between 25 and 26 (not included), Math.ceil() returns 26 because it is always rounded up. Maht.round() returns 26 only when the value is greater than or equal to 25.5. Otherwise, it returns 25. Finally, Math.floor() returns 25 for all values between 25 and 26 (not included).

```4.random method
```

The Math.random() method returns a random number in the range of 0 to 1, including 0 but not 1. This method is very convenient for web pages that want to display random quotes or random news. You can use the following formula to randomly select a number from a group of integers using Math.random():

```nuber = Math.floor(Math.random()*total_nuber_of_choices + first_possible_vaue)
```

The Math.floor() method is used here. Because Math.random() always returns a decimal, even multiplying by a number plus a number is a decimal. Therefore, if you want to randomly select a number from 1 to 10, the code is as follows:

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

In this way, there are 10 possible values (1 ~ 10), of which the minimum value is 1. If you want to select a value in the range of 2 · 10, the code can be written as follows:

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

There are only 9 numbers in 2 ~ 10, so the total_number_of_choices is 9, and the first_possible_value is 2. Many times, it may be more convenient to calculate the optional total and the minimum possible value through functions, such as:

```function selectFrom(lowerValue,upperValue){
let choices = upperValue - lowerValue + 1;
retyrn Matth.floor(Math.random() * choices + lowerValue);
}
let num = selectFrom(2,10);
console.log(num);  //Values in the range of 2 to 10, including 2 and 10
```

Here, the function selectFrom () receives two parameters; The minimum and maximum values that should be returned. By subtracting the two values and adding 1 to get the optional total, and then apply the above formula. Therefore, you can select a value from the range of 2 ~ 10 (inclusive) by calling selectFrom (2,10). 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[selectFrom(0,colors,length-1)];
```

In this example, the second parameter passed to selectFrom () is the array length minus 1, even if the array has the largest index value.

``` Math There are also many methods involving various simple or high-order operations.
```

Tags: Javascript html5

Posted on Sat, 16 Oct 2021 02:00:57 -0400 by icicleman