Math (arithmetic objects) performs common arithmetic tasks.
1. Properties of the Math object
// The base of the natural logarithm, which is the value of the constant e // Natural logarithm of 10 // Natural logarithm of 2 // 2 logarithm of base e // 10 logarithm of base e // the value of π // The square root of 1/2 (that is, the reciprocal of the square root of 2) // square root of 2
2, Min (), Max (), and apply () methods
Used to determine the minimum and maximum values in a set of values.
Cases:
var max = Math.max (1, 2, 3, 4, 5); // 5 var min = math.min (1, 2, 3, 4, 5); // 1 var values = [1, 2, 3, 4, 5]; var max = Math.max.apply (Math, values); // 5 var min = Math.min.apply (Math, values); // 1
3. Rounding Method
(1) Math.ceil () performs an upward rounding.
(2) Math.floor () performs a downward rounding.
(3) Math.Round () performs standard rounding, that is, rounding.
Cases:
Console.log (Math.ceil (2.1)); // 3Console.log (Math.floor (3.9)); // 3Console.log (Math.Round (3.3)); // 3
4. Random () method
Returns a random number greater than or equal to 0 less than 1.
Cases:
Console.log (Math.floor (Math.random () * 10 + 1)); // values from 1 to 10
5. Other methods
Math.Abs (num); // returns the absolute value of Num Math.exp (num); // returns the NUM power of MATH.E Math.log (num); // returns the natural logarithm of Num Math.pow (num, power); // returns the power sub power of Num MATH.SQRT (num); // returns the square root of Num Math.acos (x); // returns the inverse cosine value of x ...
JavaScript Math Object