[JavaScript] A complete collection of common methods of JavaScript Math objects, such as abs, sqrt, floor, trunc, random, round, acos, asin (including functions, syntax, parameter analysis, detailed examples)

1. abs() method

Function: Returns the absolute value of a number. Returns NaN if x is not a number, and 0 if x is null.
grammar:Math.abs(x)

parameter describe
x required. Must be a numeric value.

example:

let test=Math.abs(-1)
console.log(test); //输出结果:1

2. acos() method

Function: It can return the arc cosine of a number. If the parameter x exceeds the range of -1.0 ~ 1.0, the browser will return NaN. If the parameter x takes the value -1, then PI is returned.
Syntax: Math.acos(x)
Example:

parameter describe
x required. Must be a number between -1.0 ~ 1.0

example:

let test=Math.acos(0.5);
console.log(test);//输出结果:1.0471975511965979

3. asin() method

Function: Return the radian value between -PI/2 and PI/2. If the parameter x exceeds the range of -1.0 ~ 1.0, the browser will return NaN. If the parameter x takes the value 1, then PI/2 will be returned.
grammar:Math.asin(x)

parameter describe
x required. Must be a numeric value between -1.0 and 1.0.

example:

let test=Math.asin(0.5);
console.log(test);//输出结果:0.5235987755982989

4.atan() method

Function: Return the arctangent of x with a value between -PI/2 and PI/2 radians.
grammar:Math.atan(x)

parameter describe
x required. Must be a numeric value.

example:

let test=Math.atan(2);
console.log(test);  //输出结果:1.1071487177940904

5.atan2() method

Function: Return the plane angle (radian value) between the line segment from the origin (0,0) to the point (x,y) and the positive direction of the x-axis, that is, Math.atan2(y,x).
grammar:Math.atan2(y,x)

parameter describe
x must. a number representing the x-coordinate
y must. a number representing the y coordinate

example:

let test=Math.atan2(1,1);
console.log(test);  //输出结果:0.7853981633974483

6. ceil() method

Function: A number can be rounded up.
grammar:Math.ceil(x)

parameter describe
x required. Must be a numeric value.

example:

let test=Math.ceil(5.1);
console.log(test); //输出结果:6

7. cos() method

Function: Returns the cosine of a number. cos() returns a number between -1.0 and 1.0.
grammar:Math.cos(x)

parameter describe
x required. Must be a numeric value.

example:

let test=Math.cos(Math.PI);
console.log(test); //输出结果:-1

8. exp() method

Function: Return the value of e raised to the power of x. E is the natural base (approx. 2.7183).
grammar:Math.exp(x)

parameter describe
x e to the power of x

example:

let test=Math.exp(1);
console.log(test); //输出结果:2.718281828459045

9. floor() method

Function: Returns the largest integer less than or equal to x.
grammar:Math.floor(x)

parameter describe
x required. Any numeric value or expression.

example:

let test=Math.floor()(1.2);
console.log(test);  //输出结果:1 

10. log() method

Function: Returns the natural logarithm of a number (based on E). If x is negative, returns NaN. If x is 0, return -Infinity.
grammar:Math.log(x)

parameter describe
x required. Any numeric value or expression.

example:

let test=Math.log(Math.PI);
console.log(test);  //输出结果:1.1447298858494002

11. max() method

Function: Returns the number with the larger value among two or more specified numbers.
grammar:Math.max(n1,n2,n3,...,nX)

parameter describe
n1,n2,n3,…,nX optional. 1 or more values.

example:

let test=Math.max(-1,0,-2,3,2);
console.log(test);  //输出结果:3

12. min() method

Function: Returns the number with the smaller value among two or more specified numbers.
grammar:Math.min(n1,n2,n3,...,nX)

parameter describe
n1,n2,n3,…,nX optional. 1 or more values.

example:

let test=Math.min(-1,0,-2,3,2);
console.log(test);  //输出结果:-2

13.pow() method

Function: Return x raised to the power of y.
grammar:Math.pow(x,y)

parameter describe
x required. Base number. Must be a number.
y required. Power. Must be a number.

example:

let test=Math.pow(2,3);
console.log(test); //输出结果:8

14. random() method

Function: Return a random number between 0 (inclusive) and 1 (exclusive).
Syntax: Math.random()
Example:

let test=Math.random(2,3);
console.log(test);  

15. round() method

Function: Round a number to the nearest integer
Syntax: Math.round(x)
Example:

let test=Math.round(2.1);
console.log(test);  //输出结果:2

16. sin() method

Function: Return the sine value of the parameter x. The return value is between -1.0 and 1.0
Syntax:Math.sin(x)

parameter describe
x required. An angle expressed in radians. Multiply the angle by 0.017453293 (2PI/360) to convert to radians.

example

let test=Math.sin(2);
console.log(test);  //输出结果:0.9092974268256817

17. sqrt() method

Function: Return the square root of a number
Syntax:Math.sqrt(x)

parameter describe
x required. Must be a number greater than or equal to 0.

example:

let test=Math.sqrt(4);
console.log(test);  //输出结果:2

18. tan() method

Function: Returns a number representing the tangent of an angle.
grammar:Math.tan(x)

parameter describe
x required. An angle expressed in radians. Multiply the angle by 0.017453293 (2PI/360) to convert to radians.

example:

let test=Math.tan(90);
console.log(test);  //输出结果:1.995200412208242

19. tanh() method

Function: Return the hyperbolic tangent function value of a number
Syntax:Math.tanh(x)

parameter describe
x required. The number to be calculated.

example:

let test=Math.tanh(1);
console.log(test); //输出结果:0.7615941559557649

20.trunc() method

Function: Remove the decimal part of the number and keep only the integer part
Syntax:Math.trunc(x)

parameter describe
x any number

example:

 let test=Math.trunc(1.111);
console.log(test);  //输出结果:1

in conclusion

  This article mainly summarizes some commonly used methods of JavaScript Math objects. If there is any error or imperfection, please advise, thank you!

Guess you like

Origin blog.csdn.net/m0_46533551/article/details/129361988
Recommended