Math Object
The Math object is a built-in object that you have learned in high school math class. It knows all the formulas that solve the most complex mathematical problems, and if you give it the numbers to deal with, you can calculate the result.
The Math object has several attributes, mainly the specialized values of the mathematical world. The following table classes have these properties:
Property |
Description |
E |
Value E, the bottom of the natural logarithm |
LN10 |
10 of the natural logarithm |
LN2 |
2 of the natural logarithm |
Log2e |
Logarithm with 2 as the base E |
log10e |
Logarithm with 10 as the base E |
Pi |
Value π |
Sqrt1_2 |
Square root of 1/2 |
SQRT2 |
Square root of 2 |
Although the meaning and usage of these values are beyond the scope of this book, if you know what they are, you can use them when you need them.
The Math object also includes a number of methods that are specifically designed to perform simple and complex mathematical computations.
Method min () and Max () are used to determine the maximum and minimum values in a set of numbers. Both of these methods can accept any number of parameters:
For numbers 3, 54, 32, and 16,max () return 54,min () returns 3. Using these methods, you can avoid using loops or if statements to determine the maximum value in a set of numbers.
Another method abs () returns the absolute value of the number. An absolute value is a positive version of a negative number (the absolute value of a positive number is itself).
In this example, ABS (-1) returns 1,abs (1) and returns 1.
The next set of methods is used to round decimals to integers. There are three methods to handle rounding operations, namely ceil (), floor (), and Round (), which have different processing methods:
The Q Method Ceil () represents a rounded up function that always rounds the number up to the nearest value.
The Q Method Floor () represents a downward rounding function that always rounds the number down to the nearest value.
The Q method Round () represents a standard rounding function, rounded up if the difference between the number and the next integer is not more than 0.5, or rounded down. This is the rounding rule that was learned in junior high school.
To illustrate how each method is handled, consider using a value of 25.5:
For Ceil () and round (), pass 25.5, return 26, and floor () returns 25. Be careful not to use these methods alternately, because you may end up with a result that does not match expectations.
Another set of methods is associated with the use of indices. These methods include exp (), which is used to raise the MATH.E to the specified power log () to return the natural logarithm of a particular number; POW () is used to raise the specified number to the specified power sqrt () to return the square root of the specified number.
Method exp () and log () essentially function instead, exp () raises the MATH.E to a particular power, log () to determine how many exponents of MATH.E are equal to the specified value. For example:
Here, first with exp () to the MATH.E to 10 power, and then log () return 10, that is equal to the number of inum required exponent. A lot of people are confused about it. Students in high school and math departments around the world have been stumped by such problems. If you don't know anything about the natural logarithm, you probably never need to write code for it.
The Method Pow () is used to raise the number to a specified power, such as to raise 2 to a power of 10 (expressed in mathematics as 2):
The first parameter of POW () is the cardinality, in this case 2. The second argument is the power to be raised, in this case 10.
MATH.E is not recommended as the base of the Pow () method. It is best to use exp () to perform a Huang operation on MATH.E because it is a private operation and the calculated value is accurate.
The last method in this set of methods is sqrt (), which returns the square root of the specified number. It has only one parameter, the number that requires the square root. To require a square root of 4, you need only one line of code:
Of course, the square root of 4 is 2, which is the output of this line of code.
You might ask, "Why do square roots have to use indices?" In fact, the square root of a number is its power of 1/2 times. For example, 21/2 is the square root of 2.
The Math object also has a complete trigonometric method. The following table lists these methods:
Method |
Description |
ACOs (x) |
Returns the anti-cosine value of X |
ASIN (x) |
Returns the inverse chord value of x |
Atan (x) |
Returns the tangent value of X |
ATAN2 (y,x) |
Returns the inverse cosine of a y/x |
COS (x) |
Returns the cosine value of X |
Sin (x) |
Returns the sine value of x |
Tan (x) |
Returns the tangent value of X |
Even though these methods are ECMA-262 defined, the results are also determined by the implementation, because each value has a lot of calculation methods, which makes the results of different implementations produce different precision.
The last method of the Math object is random (), which returns a random number from 0 to 1, excluding 0 and 1. This is a common tool for sites that display random quotes or news on the home page. You can call the random () method in the following form to select a random number within a range:
This uses the method floor (), because random () returns a decimal value, that is, multiplying it by a number and then adding a number, the result is still a decimal value. Usually you want to select a random integer value. Therefore, you must use the floor () method. If you want to select a number between 1 and 10, the code is as follows:
The possible values are 10 (1 to 10), and the first of these values is 1. If you want to select a value between 2 and 10, the code is as follows:
From 2 to 10, there are only 9 digits, so the total number of options is 9, where the first value is 2. Many times, it is easier to use a function that calculates the total number of options and the first available value:
Using functions, you can easily select random items in an array:
Here, the second parameter of the Selectfrom () function is the length of the array minus 1, the position of the last element in the array.