This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Writing in the front
Math is a JavaScript native object that provides a variety of mathematical capabilities. This object is not a constructor and cannot generate instances, and all properties and methods must be called on the Math object.
Static attributes
Static properties of the Math object that provide the following mathematical constants.
Math.E
: constante
.Math.LN2
The natural logarithm of: 2.Math.LN10
: The natural logarithm of 10.Math.LOG2E
: base 2e
The logarithm.Math.LOG10E
: base 10e
The logarithm.Math.PI
: constantPI.
.Math.SQRT1_2
: square root of 0.5.Math.SQRT2
: the square root of 2.
Math.E // 2.718281828459045 math. LN2 // 0.6931471805599453 math. LN10 // 2.302585092994046 math. LOG2E // LOG10E // 0.4342944819032518 math. PI // 3.141592653589793 math. SQRT1_2 // 0.7071067811865476 Math. SQRT2 / / 1.4142135623730951Copy the code
These properties are read-only and cannot be modified.
A static method
The Math object provides the following static methods.
Math.abs()
: the absolute valueMath.ceil()
: Round upMath.floor()
: Round downMath.max()
The maximum value of:Math.min()
: the minimumMath.pow()
: power operationMath.sqrt()
: the square rootMath.log()
: Natural logarithmMath.exp()
:e
The index ofMath.round()
: Round offMath.random()
Random number:
Math.abs()
The math. abs method returns the absolute value of the parameter value.
Math.abs(1) // 1
Math.abs(-1) // 1
Copy the code
Math. Max (), Math. Min ()
The math.max method returns the largest of the parameters, and math.min returns the smallest. If the argument is empty, math. min returns Infinity and math. Max returns -infinity.
Math.max(2, -1, 5) // 5
Math.min(2, -1, 5) // -1
Math.min() // Infinity
Math.max() // -Infinity
Copy the code
Math. The floor (), Math. Ceil ()
The math.floor method returns the maximum integer (floor value) less than or equal to the parameter value.
Math.floor(3.2) // 3
Math.floor(-3.2) // -4
Copy the code
The math.ceil method returns the minimum integer (ceiling value) greater than or equal to the parameter value.
Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3
Copy the code
These two methods can be combined to implement a function that always returns the integer part of a numeric value.
function ToInteger(x) { x = Number(x); return x < 0 ? Math.ceil(x) : Math.floor(x); } ToInteger(3.2) // 3 ToInteger(3.5) // 3 ToInteger(3.8) // 3 ToInteger(-3.2) // -3 ToInteger(-3.5) // -3 / / - 3 ToInteger (3.8)Copy the code
In the above code, the ToInteger function always returns the integer part of a number, whether positive or negative.
Math.round()
The math.round method is used for rounding.
Math.round(0.1) // 0 math.round (0.5) // 1 math.round (0.6) // 1 // equivalent to math.floor (x + 0.5)Copy the code
Notice how it handles negative numbers (mostly 0.5).
Math. Round (1.1) Math. / / - 1 round (1.5) / / - 1 Math. Round / / - 2 (1.6)Copy the code
Math.pow()
The math.pow method returns a power operation based on the first argument and an exponent as the second argument.
// Equivalent to 2 ** 2 math.pow (2, 2) // 4 / equivalent to 2 ** 3 math.pow (2, 3) // 8Copy the code
Here’s how to calculate the area of a circle.
var radius = 20;
var area = Math.PI * Math.pow(radius, 2);
Copy the code
Math.sqrt()
The math. SQRT method returns the square root of the parameter value. If the argument is a negative value, NaN is returned.
Math.sqrt(4) // 2
Math.sqrt(-4) // NaN
Copy the code
Math.log()
The math.log method returns the natural log base E.
Math.log(math.e) // 1 math.log (10) // 2.302585092994046Copy the code
If you want to compute logarithms base 10, you can compute the natural logarithm using math.log and divide by math.log 10; Take the logarithm base 2, divide by math.ln 2.
Math.log(100)/Math.LN10 // 2
Math.log(8)/Math.LN2 // 3
Copy the code
Math.exp()
The math.exp method returns the constant e to the argument power.
Math. Exp (1) // 2.718281828459045 Math. Exp (3) // 20.085536923187668Copy the code
Math.random()
Math.random() returns a pseudo-random number between 0 and 1, which may be equal to 0 but must be less than 1.
Math. The random () / / 0.7151307314634323Copy the code
The random number generating function of any range is as follows.
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
getRandomArbitrary(1.5, 6.5)
// 2.4942810038223864
Copy the code
An arbitrary range of random integer generating functions is shown below.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomInt(1, 6) // 5
Copy the code
The following is an example of a random character returned.
function random_str(length) {
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
ALPHABET += '0123456789-_';
var str = '';
for (var i = 0; i < length; ++i) {
var rand = Math.floor(Math.random() * ALPHABET.length);
str += ALPHABET.substring(rand, rand + 1);
}
return str;
}
random_str(6) // "NdQKOr"
Copy the code
In the above code, the random_str function takes an integer as an argument and returns a string of the specified length made up of random characters in the variable ALPHABET.
Trigonometric method
The Math object also provides a series of trigonometric methods.
Math.sin()
: Returns the sine of the parameter (parameter is radian value)Math.cos()
: Returns the cosine of the parameter (parameter is radian value)Math.tan()
: Returns the tangent of the argument (parameter is radian value)Math.asin()
: Returns arcsine of the argument (return value is radian value)Math.acos()
: Returns the arccosine of the argument (return value is radian value)Math.atan()
: Returns the arctangent of the argument (return value is radian value)
Math.sin(0) // 0 math.tan (0) // 0 math.sin (math.pi / 2) // 1 math.asin (1) // 1.5707963267948966 Math.acos(1) // 0 math.atan (1) // 0.7853981633974483Copy the code