Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the math method of es6

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points about how to use es6's math method. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Math methods include: 1, exponential methods, including pow (), sqrt (), etc.; 2, logarithmic methods, including log (), log10 (), etc.; 3, algebraic methods, including abs (), sign (), etc.; 4, trigonometric functions, including sin (), cos (), etc.; 4, random (), returns pseudo-random numbers.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

ES6 Math (Mathematical object)

Mathematical objects provide you with the properties and methods of mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static, and you can use Math as an object without creating it.

Methods of math object

1. Index method

The basic exponential method is Math.pow () and has convenient functions for square root, cubic root, and the power of e, as shown in the following table:

Method to describe Math.pow (x, y)

Returns x to the y power

Math.sqrt (x)

Returns the square root of the number x

Math.cbrt (x)

This method returns the cube root of the number x

Math.exp (x)

Equivalent to Math.pow (Math.E,x)

Math.expm1 (x)

Equivalent to Math.exp (x)-1

Math.hypot (x1, x2...)

Returns the square root of the sum of the parameters

2. Logarithmic method

The basic natural logarithm method is Math.log (). In JavaScript, "log" means "natural logarithm". For convenience, ES6 introduces Math.log10.

Method to describe Math.log (x)

The natural logarithm of x

Math.log10 (x)

Logarithm with a base of 10

Math.log2 (x)

The logarithm of cardinality 2 of x

Math.log1p (x)

Natural logarithm of 1 + x

3. Miscellaneous algebraic methods

The following is a list of various algebraic methods and their descriptions.

Method to describe Math.abs (x)

The absolute value of x

Math.sign (x)

The sign of x: if x is negative,-1; if x is positive, then 1; if x is 0, 0

Math.ceil (x)

Upper limit of x: the smallest integer greater than or equal to x

Math.floor (x)

The bottom of x: the largest integer less than or equal to x

Math.trunc (x)

The integer portion of x (all decimal places are deleted)

Math.round (x)

X rounded to the nearest integer

Math.min (x1, x2...)

Returns the minimum parameter

Math.max ((x1, x2,...)

Returns the minimum parameter

4. Trigonometric function

Method to describe Math.sin (x)

The sine of x radians

Math.cos (x)

CoSine of x radians

Math.tan (x)

Tangent of x radians

Math.asin (x)

Arcsine (arcsin) of x (causing radians)

Math.acos (x)

Inverse cosine (arccos) of x (causing radians)

Math.atan (x)

Arc tangent (arctan) of x (causes radians)

Math.atan2 (y, x0)

Counterclockwise angle (radians) from x-axis to point (XBI y)

5. Math.random ()

The Math.random () function returns a pseudorandom number between 0 (inclusive) and 1 (exclusive).

Example: pseudorandom number generation (PRNG)

Var value1 = Math.random (); console.log ("First Test Value:" + value1); var value2 = Math.random (); console.log ("Second Test Value:" + value2); var value3 = Math.random (); console.log ("Third Test Value:" + value3); var value4 = Math.random (); console.log ("Fourth Test Value:" + value4)

Output

First Test Value: 0.5782922627404332Second Test Value: 0.5624510529451072Third Test Value: 0.9336334094405174Fourth Test Value: 0.4002739654388279 is all the content of this article "how to use es6's math method". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report