Home  >  Article  >  Web Front-end  >  What are the es6 math methods?

What are the es6 math methods?

青灯夜游
青灯夜游Original
2022-03-23 19:51:022919browse

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.

What are the es6 math methods?

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

ES6 Math (Math Object)

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

Math object methods

1. Exponential method

The basic exponential method is Math. pow(), and has convenience functions for square roots, cube roots, and powers of e, as shown in the following table:

Method Description
Math.pow(x, y)

Return x to the power of y

Math.sqrt(x )

Returns the square root of the number x

Math.cbrt(x)

This method returns the number x The cube root of

Math.exp(x)

is equivalent to Math.pow(Math.E,x)

Math.expm1(x)

is equivalent to Math.exp(x) - 1

Math.hypot(x1, x2,...)

Returns the square root of the sum of parameters

2. Right Math method

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

Method Description
Math.log(x)

Natural logarithm of x

Math.log10(x)

Logarithm of base 10

Math.log2(x)

The logarithm of base 2 of x

Math.log1p(x)

1 The natural logarithm of x

3. Miscellaneous algebraic methods

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

##Math.ceil(x)Math.floor(x)Math.trunc(x)Math. round(x)##Math.min(x1, x2,...)Return the minimum parameter##Math.max((x1, x2,...)4. Trigonometric functions
Method Description
Math.abs(x)

The absolute value of x

Math.sign(x)

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

#The upper limit of x: the minimum value greater than or equal to x Integer

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

The integer part of x (all decimal places removed)

x rounds to the nearest integer

Return the minimum parameter

MethodDescription Math.sin(x)Math.cos( x)Math.tan(x)Math.asin(x)Math.acos(x)Math.atan (x)Math.atan2(y, x0)5, Math.random()
x sine of radians

xThe cosine of radians

xThe tangent of radians

The inverse sine (arcsin) of x (resulting in radians)

The inverse cosine (arccos) of x (resulting in radians)

Arctan of x (resulting in radians)

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

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

Example: Pseudo-random 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.5782922627404332
Second Test Value : 0.5624510529451072
Third Test Value : 0.9336334094405174
Fourth Test Value : 0.4002739654388279

[Related recommendations:

javascript video tutorial

,

web front-end

The above is the detailed content of What are the es6 math methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn