Complex operations in js

 

Math object

Math objects are used to perform mathematical tasks.

 

 

math object properties

Attributes
E Returns the arithmetic constant e, the base of the natural logarithm (approximately equal to 2.718).

LN2 returns the natural logarithm of 2 (approximately equal to 0.693).

LN10 returns the natural logarithm of 10 (approximately equal to 2.302).

LOG2E Returns the base 2 logarithm of e (approximately equal to 1.414).

LOG10E Returns the base 10 logarithm of e (approximately equal to 0.434).

PI returns Pi (approximately equal to 3.14159).

SQRT1_2 Returns the inverse of the square root of 2 (approximately equal to 0.707).

SQRT2 returns the square root of 2 (approximately equal to 1.414).

 

Math.abs(x) Returns the absolute value of a number.
Math.acos(x) returns the arc cosine of a number.
Math.asin(x) Returns the arcsine of a number.
Math.atan(x) Returns the arctangent of x as a value between -PI/2 and PI/2 radians.
Math.atan2(y,x) Returns the angle (between -PI/2 and PI/2 radians) from the x-axis to the point (x,y).
Math.ceil(x) rounds up the logarithm.
Math.cos(x) returns the cosine of a number.
Math.exp(x) returns the exponent of e.
Math.floor(x) rounds down the logarithm.
Math.log(x) returns the natural logarithm (base e) of a number.
Math.max(x,y) returns the highest value of x and y.
Math.min(x,y) returns the lowest value of x and y.
Math.pow(x,y) returns x raised to the y power.
Math.random() returns a random number between 0 and 1.
Math.round(x) rounds a number to the nearest whole number.
Math.sin(x) returns the sine of a number.
Math.sqrt(x) returns the square root of a number.
Math.tan(x) returns the tangent of an angle.
Math.toSource() returns the source code for this object.
Math.valueOf() returns the original value of the Math object.

 max output 15

var x = 15;
var y = 10;
var arr = Math.max(x,y)
console.log(JSON.stringify(arr))

 

Minimum min output 10

var x = 15;
var y = 10;
var arr = Math.min(x,y)
console.log(JSON.stringify(arr))
round output 16 var x = 15.8;
var y = 10;
var z = 100 ;
var arr = Math.round(x)
console.log(JSON.stringify(arr)) rounds down to output 15 var x = 15.8;
var y = 10;
var z = 100;
var arr = Math.floor(x)
console.log(JSON.stringify(arr)) rounds up to output 16 var x = 15.2;
var y = 10;
var z = 100;
var arr = Math.ceil(x)
console.log(JSON.stringify(arr) )
var a = Math.abs(-2 ), // |-2|    abs(x) returns the absolute value of x
b = Math.exp(2 ), // e 2       exp(x) returns e raised to the power of x 
c = Math.log(2 ), // log e 2       log(x) returns log base e, x Logarithm 
d = Math.pow(2,3 ), // 2 3       pow(x,y returns x raised to the y power 
e = Math. sqrt(9); // √9 sqrt(x) returns the square root of x
var a = Math.ceil(1.4 ), // =>2    ceil(x) round up
b = Math.floor(1.6 ), // =>1    floor(x) round down
c = Math.round(1.5 ), // =>2    round(x) rounding
d = Math.random(); // 0~1 random number random() returns (0,1) randomly

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780235&siteId=291194637
Recommended