Home  >  Article  >  Web Front-end  >  Detailed introduction to math objects in native js

Detailed introduction to math objects in native js

零下一度
零下一度Original
2017-06-27 15:51:041480browse

This article refers to the detailed arrangement made by MDN for your convenience.
Math is also a built-in object that provides properties and methods for mathematical constants and mathematical functions, rather than a function object.

Different from other global objects, Math is not a constructor. All properties and methods of Math are static. The constant pi you use can be represented by Math.PI, and x is used as the parameter Math. sin(x) calls the sin function. Constants in JavaScript are defined as full-precision real numbers.

Attributes:

Math.E
Euler’s constant is also a natural logarithm The base is approximately equal to the natural logarithm of 2.718.
Math.LN2
2, which is approximately equal to the natural logarithm of 0.693.
Math.LN10
10, which is approximately equal to 2.303.
Math.LOG2E
The logarithm of E with base 2 is approximately equal to 1.443.
Math.LOG10E
The logarithm of E with base 10 is approximately equal to 0.434.
Math.PI
Pi, a The ratio of the circumference and diameter of a circle is approximately equal to the square root of 3.14159.
Math.SQRT1_2
1/2, which is approximately equal to the square root of 0.707.
Math.SQRT2
2, which is approximately equal to 1.414.

Method:

It should be noted that:
Trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2( )) returns the value in radians. Radians can be converted to degrees by division (Math.PI/180), or by other methods.
Many mathematical functions have a precision, and the precision is different in different environments. This means that different browsers will give different results, and even the same JS engine will give different results under different OS or architectures.

Math.ceil(x)
Returns the integer value of x after rounding up.
Math.floor(x)
Returns the integer value of x after rounding down.
Math.round(x)
Returns the rounded integer.
Math.sqrt(x)
Returns the square root of x. If the parameter number is a negative value, sqrt returns NaN.
Math.tan(x)
Returns the tangent value of x, x represents an angle (unit: radians)
Math.abs(x)
Returns the absolute value of x. Pass in a non-number A string in the form or an undefined/empty variable will return NaN. Passing in null will return 0.
Math.sin(x)
Returns a value between -1 and 1, representing the sine value of a given angle (unit: radians)
Math.acos(x)
Returns a number The arc cosine of (in radians). The acos method takes a number from -1 to 1 as a parameter and returns a value from 0 to pi (radians). If the passed parameter value exceeds the limited range, NaN will be returned
Math.asin(x)
Returns the arc sine of a value (unit is radians). The asin method accepts a value between -1 and 1 as a parameter and returns a value between -π/2 and π/2 radians. If the accepted parameter value is out of range, NaN is returned.
Math.atan(x)
Returns the arctangent of a value (in radians). Returns the arctangent of x as a value between -PI/2 and PI/2 radians.
Math.atan2(x, y)
Returns the arctangent value of y/x. The atan2 method returns a value between -pi and pi, indicating the offset angle corresponding to the point (x, y). This is the counterclockwise angle, in radians, between the positive X-axis and the line connecting point (x, y) to the origin. Note the parameters this function accepts: the y-coordinate is passed first, followed by the x-coordinate.
atan2 accepts separate x and y parameters, while atan accepts the ratio of the two parameters
Math.cos(x)
Returns the cosine value of x. Returns a value between -1 and 1, indicating The cosine of the angle in radians.
Math.exp(x)
Returns Ex, when x is a parameter, E is Euler's constant (2.718...), the base of natural logarithms.
Math.log(x)
Returns the natural logarithm of a number. If the specified number is negative, the return value is NaN.
Math.max([x[,y[,…]]])
Returns the maximum value from 0 to multiple values.
If there are no parameters, the result is - Infinity.
If any parameter cannot be converted to a numeric value, the result is NaN.
Math.min([x[,y[,…]]])
Returns the smallest value from 0 to multiple values. Math.min is often used to clip a value so that it is always less than or Equal to a certain boundary value
If there are no parameters, the result is Infinity.
If any parameter cannot be converted to a numeric value, the result is NaN.
Math.pow(x,y)
Returns the y power of x.
Math.random()
Returns a pseudo-random number between 0 and 1 (greater than or equal to 0, less than 1) , using the current time as the random number seed
Math.floor(Math.random() * (max - min + 1) + min); // Return an integer random number between min and max

The following ES6 new additions:

log10(), log2(), log1p(), expm1(), cosh(), sinh(), tanh(), acosh(), asinh(), atanh(), hypot(), trunc() , sign(), imul(), around(), cbrt() clz32()
Math.acosh(x)
Returns the inverse hyperbolic cosine of x.
Math.asinh(x)
Returns the inverse hyperbolic sine of x.
Math.atanh(x)
Returns the inverse hyperbolic tangent of x.
Math.cbrt(x)
Returns the cube root of x.
Math.clz32(x)
Returns the number of leading zeroes of a 32-bit integer.
Math.cosh(x)
Returns the hyperbolic cosine of x.
Math.expm1 (x)
Returns the value of exp(x)-1.
Math.fround(x)
Returns the nearest single precision float representation of a number.
Math.hypot([x[, y[,…]]])
Returns the square root of the sum of squares of its arguments.
Math.imul(x)
Returns the result of a 32-bit integer multiplication.
Math.log1p(x)
Returns the natural logarithm of 1 + x (loge, also ln) of a number.
Math.log10(x)
Returns the base 10 logarithm of x.
Math.log2(x)
Returns the base 2 logarithm of x.
Math.sign(x)
Returns the sign function of x to determine whether x is a positive number, a negative number or 0.
Math.sinh (x)
Returns the hyperbolic sine value of x.
Math.tanh(x)
Returns the hyperbolic tangent value of x.
Math.trunc(x)
Returns the integer of x part, remove decimals.

The above is the detailed content of Detailed introduction to math objects in native js. 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