Method of the Math object
Compare the most valuable methods
There are two ways to compare maximum values, the Max () and min() methods.
1. The Max () method compares the maximum value in a set of values and returns the maximum value.
Var maxnum = Math. Max (12,6,43,58,70); alert(maxnum); / / "70"Copy the code
2. The min() method compares the minimum value in a set of values and returns the minimum value.
Var minnum = Math. Min (12,6,43,58,70); alert(minnum); / / "6"Copy the code
Numerical rounding method
The 1 ceil() method rounds the value up to the nearest integer.
Alert (math.h ceil (6.1)); / / "7" alert (math.h ceil (6.5)); / / "7" alert (math.h ceil (6.9)); / / "7"Copy the code
No matter what the decimal place after 6 is, the method always rounds up, resulting in 7.
2.2 Floor () method, which rounds the value down to the nearest integer.
Alert (Math) floor (6.1)); / / "6" alert (Math. Floor (6.5)); / / "6" alert (Math. Floor (6.9)); / / "6Copy the code
No matter what the decimal place after 6 is, the method always rounds down and the result is 6.
3. The round() method rounds the value to the nearest whole number.
Alert (Math. Round (6.1)); / / "6" alert (Math. Round (6.5)); / / "7" alert (Math. Round (6.9)); / / "7"Copy the code
This method is rounded in accordance with the rounding rules we learned in elementary school.
3. Random number generation method
The random() method returns a random number greater than or equal to 0 and less than 1. The range of random numbers is represented as [0,1), including 0 but not including 1.
Here is a formula: return value = math.floor (math.random () * total number of possible values + the first possible value), which can be used to return a random number within some integer range.
We notice that the above formula calls the floor() method because random() always returns a decimal and we want to return an integer, so we have to round its return value.
For example, if we want to randomly generate integers between 1 and 10, [1,10], including 1 and 10, we can write the following code using the formula:
var num = Math.floor(Math.random() * 10 + 1);
There are 10 numbers between [1,10], so the total number of possible values is 10, then random()*10, that is, random numbers between [0,10) are generated.
The first possible value is 1, then random()*10+1, which generates a random number between [1,11);
At this point, the smallest integer it produces is 1, but instead of 10, the largest integer it produces is a decimal like 10.xxxxxxxxxx, so use floor() to round it down so that the largest integer it produces is 10.
If you want to randomly generate integers between [5,10], you can write code like this using the formula:
var num = Math.floor(Math.random() * 6 + 5);
There are 6 numbers between [5,10], so the total number of possible values is 6, then random()*6; The first possible value is 5, so random()*6+5; Finally, round it down with floor().
Other cases can be calculated by their own examples.
So we can encapsulate a random number function as follows:
function random(n,m){ var abs = Math.abs(m-n) var min = Math.min(m,n) var res = parseInt(Math.random()*abs)+min return res; } Random (1,10)//1~10 random numberCopy the code
4. Other calculation methods
The following methods are used for all kinds of simple and complex calculations.
Math.abs(num) returns the absolute value of num
Math.exp(num) returns math.e to the num power
Math.log(num) returns the natural logarithm of num
Math.pow(num,power) returns the power of num
Math.sqrt(num) returns the square root of num
Math.acos(x) returns the inverse cosine of x
Math.asin(x) returns the arcsine of x
Math.atan(x) returns the arctangent of x
Math.atan2(y,x) returns the arctangent of y/x NBSP;
Math.cos(x) returns the cosine of x
Math.sin(x) returns the sine of x
Math.tan(x) returns the tangent of x