Home  >  Article  >  Web Front-end  >  10 Math object methods that make you more efficient, come and collect them!

10 Math object methods that make you more efficient, come and collect them!

青灯夜游
青灯夜游forward
2021-05-27 11:56:531946browse

This article will introduce to you 10 Math object methods that can help you get twice the result with half the effort. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

10 Math object methods that make you more efficient, come and collect them!

math Pairs in JavaScript allow us to perform some mathematical operations on pairs. It has properties and methods for mathematical constants and functions. In today's article, we will introduce some useful methods of the Math object.

1. Math.min()

Math.min() is a function in the JS math library, used to convert all passed The minimum of the values ​​is returned to this method.

Math.min(0, 150, 30, 20, -8, -200) // -200

2. Math.max()

Math.max() method can return the larger of the two specified numbers. The value of that number.

Math.max(0, 150, 30, 20, -8, -200) // 150

3. Math.round()

Math.round() The function returns a number rounded to the nearest integer.

Math.round(4.7) // 5
Math.round(4.4) // 4

4. Math.sqrt()

Math.sqrt() The function returns the square root of a number, that is:

10 Math object methods that make you more efficient, come and collect them!

Math.sqrt(64) // 8
Math.sqrt(25) // 5

5. Math.pow()

Math.pow() The function returns the exponent of the base (base) (exponent) power, that is:

Math.pow(8, 2) // 64

6. Math.floor()

Math.floor() Returns less than or equal to one The largest integer for a given number.

Math.floor(4.7) // 4
Math.floor(8.6) // 8

7. Math.random()

Math.random() The function returns a floating point, pseudo-random number in the range from0 to less than 1, that is, from 0 (including 0) upwards, but not including 1 (exclude 1) and then you can zoom to the desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be selected or reset by the user.

Math.random()  // 0.15497907645259867

8. Math.cos()

Math.cos() The function returns the cosine of a value.

Math.cos(0, Math.PI / 180)  // 1

9. Math.sin()

Math.sin() The function returns the sine of a value.

Math.sin(90 * Math.PI / 180) // 1

Math.ceil() The function returns the smallest integer greater than or equal to a given number.

Math.ceil(4.4) // 5

Summary

The JS Math object is very powerful and useful when performing some numerical operations. In addition to the above 10 methods, the Math object has many others. The method is left to everyone to read the document by themselves. That’s it for today’s sharing. Thank you for watching. See you in the next issue.

English original address: https://medium.com/javascript-in-plain-english/top-10-javascript-methods-of-the-math-object-ac69951386a5

Author: Mehdi Aoussiad

Translation address: https://segmentfault.com/a/1190000040071207

For more programming-related knowledge, please visit: programming video! !

The above is the detailed content of 10 Math object methods that make you more efficient, come and collect them!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete