I am small and live in Wuhan, do two years of new media, ready to use 6 months time to switch to the front end of the industry.

Lesson Objective for today

Yesterday we learned the basics of Math object properties and basic methods based on search. Today’s focus is on getting a basic understanding of the rest of the Math object’s methods based on search, followed by a detailed look at the methods of interest.

Today is the last day of ECMAScript basic learning, after which we will prepare to learn BOM ~~~, another day for learning, come on, small and !!!!


Today’s Lesson summary

  • Math object method outline
  • The random () method

Math object outline method

String cut related

The method name Method statement
Math.acos(x) Returns the inverse cosine of a number.
Math.acosh(x) Returns the inverse hyperbolic cosine of a number.
Math.asin(x) Returns the arcsine of a number.
Math.asinh(x) Returns the inverse hyperbolic sine of a number.
Math.atan(x) Returns the arctangent of a number.
Math.atanh(x) Returns the inverse hyperbolic tangent of a number.
Math.atan2(y, x) Returns the arctangent of y/x.
Math.cos(x) Returns the cosine of a number.
Math.cosh(x) Returns the hyperbolic cosine of a number.
Math.sin(x) Returns the sine of a number.
Math.sinh(x) Returns the hyperbolic sine of a number.
Math.tan(x) Returns the tangent of a number.
Math.tanh(x) Returns the hyperbolic tangent of a number.

Rounding related

The method name Method statement
Math.ceil(x) Returns the smallest integer greater than the rounded value of a number.
Math.floor(x) Returns the largest integer less than the rounded value of a number.
Math.round(x) Returns a rounded integer.

Single value basis processing correlation

The method name Method statement
Math.abs(x) Returns the absolute value of a number.
Math.sign(x) Returns the symbol of a number that tells whether it is positive, negative, or 0.
Math.fround(x) Returns the single-precision floating-point representation of the nearest number.
Math.trunc(x) Returns the integral part of a number, removing the decimal point and all subsequent parts.

Single value decomposition processing correlation

The method name Method statement
Math.cbrt(x) Returns the cube root of a number.
Math.log(x) Return the natural logarithm of a number (㏒e, as ㏑).
Math.log1p(x) Return the natural log of a sum of a number plus 1 (㏒e, as ㏑).
Math.log10(x) Return a logarithm of base 10.
Math.log2(x) Return a logarithm of base 2.
Math.sqrt(x) Returns the square root of a number.
Math.pow(x, y) Returns a number to the y power.

Multivalue processing correlation

The method name Method statement
Math. Hypot ([[x, y, [...]]]) Returns the square root of the sum of squares of all its arguments.
Math. Max ([[x, y, [...]]]) Returns zero to the maximum of multiple values.
Math. Min ([[x, y, [...]]]) Returns zero to the smallest of multiple values.

The random number

The method name Method statement
Math.random() Returns a pseudo-random number between 0 and 1.

other

The method name Method statement
Math.clz32(x) Returns the number of leading zeros of a 32-bit integer.
Math.exp(x) Return the euler constant to the parametric power, Ex, where x is the parameter and E is the Euler constant (2.718… , the base of the natural logarithm.
Math.expm1(x) Return the value exp(x) -1.
Math.imul(x, y) Returns the result of a 32-bit integer multiplication.
Math.toSource() Returns the string “Math”.

random()

Basic grammar

Math.random()
Copy the code

Return Value Description

A pseudo-random floating point number between 0 (inclusive) and 1 (exclusive).


Detailed instructions

Math.random() returns a floating point pseudo-random number in the range from 0 to less than 1, that is, up from 0 (including 0) but not including 1 (excluding 1), which can then be scaled to the desired range. Realize the initial seed selection to random number generation algorithm; It cannot be selected or reset by the user.


Matters needing attention

security

Math.random() does not provide a random number as secure as a password

You can’t use them for security matters. Web Crypto API can be used instead, and the more accurate the window. The Crypto. GetRandomValues () method.


Accuracy and accuracy

Note that since the numbers in JavaScript are IEEE 754 floating-point numbers with round-to-nearest-even behavior, the scope of the following functions (excluding math.random () itself) is not accurate.

If a very large boundary (253 or higher) is selected, the usual-excluded upper bound is calculated in rare cases.

The default mode of IEEE 754 standard is nearest rounding (rounding to the nearest even number), which differs from rounding in that rounding of.5 is round-to-nearest-even, i.e., round-to-nearest-even, for example:

Round to nearest even: Round0.5) = 0; Round(1.5) = 2; Round(2.5) = 2; Round(3.5) = 4;
Copy the code

That is: “rounding six into five even” “five even” rule: when the decimal part is exactly 0.5, if the ones place is odd, if the ones place is even, then rounding, in short, let the ones place become even.


case

Get a random number between greater than or equal to 0 and less than 1

function getRandom() {
  return Math.random();
}

Copy the code


Get a random number between two numbers

This example returns a random number between the specified values. This value is not less than min (which may be equal to), and is less than (not equal to) Max.

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min; 
}
Copy the code


Summary of today’s lesson



Today the mood

Today, I mainly studied the remaining methods of Math object based on search, and then had a detailed understanding of the methods I was interested in. In fact, I mainly learned about the Random method. Although I tried other methods, I did not write a summary, so I was lazy ~~~~

Then this ECMAScript basic learning series comes to an end, tomorrow will give a summary of this period of learning, and then start BOM la la la la ~~~~


This article is formatted using MDNICE