Talk about Math of JavaScript type system

  • 2020-11-25 07:06:33
  • OfStack

The door must read

math differs from other objects in that the Math object is a static object, not a constructor. In fact, Math is just an object namespace set by Javascript to store mathematical functions

attribute

The base of the natural logarithm, i.e. the value of the constant e (approximately 2.718)
Math. The value of the PI faction (approximately 3.14159)
console.log(Math.E);//2.718281828459045
console.log(Math.PI);//3.141592653589793
The natural log of ES25en2 2 (approximately 0.693)
The natural log of 10 (approx. 2.302)
Logarithm of Math, base 2 of e (approximately 1.414)
Log base 10 of e (approximately 0.434)
console.log(Math.LN2);//0.6931471805599453
console.log(Math.LN10);//2.302585092994046
console.log(Math.LOG2E);//1.4426950408889634
console.log(Math.LOG10E);//0.4342944819032518
The square root of Math2 (approximately 1.414)
The square root of Math.es53EN1_2 1/2, i.e. the reciprocal of the square root of 2 (approximately 0.707)
console.log(Math.SQRT2);//1.4142135623730951
console.log(Math.SQRT1_2);//0.7071067811865476

methods

These methods all involve Number() implicit type conversion; If the method scope is exceeded, NaN is returned

Math.min () returns the minimum value of a set of 1 Numbers
Math. max() returns the maximum value in a set of 1 Numbers
console.log(Math.min(1,2,3));//1
console.log(Math.max(1,2,3));//3
Math.ceil(num) rounded up to an integer
Math.floor(num) rounded down to an integer
4 is rounded to 5 as an integer
console.log(Math.ceil(12.6));//13
console.log(Math.floor(12.6));//12
console.log(Math.round(12.6));//13
Math.abs(num) returns the absolute value of num
Math. random() returns 1 random number greater than or equal to 0 and less than 1
console.log(Math.abs(-10));//10
console.log(Math.random());//0.741887615993619
Math. exp(num) returns the num power of ES111en. E
Math.log(num) returns the natural logarithm of num
Math. sqrt(num) returns the square root of num (x must be a number greater than or equal to 0)
Math.pow(num,power) returns the power power of num
console.log(Math.exp(0));//1
console.log(Math.log(10));//2.302585092994046
console.log(Math.sqrt(100));//10
console.log(Math.pow(10,2));//100
Math.sin(x) returns the sine of x
Math.cos(x) returns the cosine of x
Math.tan(x) returns the tangent of x
Math.asin(x) returns the arcsine value of x (x must be between -1 and 1)
Math.acos(x) returns the arccosine value of x (x must be a number between -1 and 1)
Math.atan(x) returns the inverse tangent of x
Math.atan2(y,x) returns the inverse tangent of y/x
console.log(Math.sin(30*Math.PI/180));//0.49999999999999994
console.log(Math.cos(60*Math.PI/180));//0.5000000000000001
console.log(Math.tan(45*Math.PI/180));//0.9999999999999999
console.log(Math.asin(1)*180/Math.PI);//90
console.log(Math.acos(1)*180/Math.PI);//0
console.log(Math.atan(1)*180/Math.PI);//45
console.log(Math.atan2(1,1)*180/Math.PI);//45

tips

[tips1] find the maximum or minimum value in the array


var values = [1,2,3,4,5,6,7,8];
var max = Math.max.apply(Math,values);//8 

[tips2] randomly selects a value from a range of integers


value = Math.floor(Math.random()* Total number of possible values  +  The first 1 A possible number )

[tips3] randomly selects a value by the minimum and maximum values


function selectFrom(lowerValue,upperValue){
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random()*choices + lowerValue);
}
var num = selectFrom(2,10);
console.log(num); 

Math object method

方法 描述
abs(x) 返回数的绝对值。
acos(x) 返回数的反余弦值。
asin(x) 返回数的反正弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x) 对数进行上舍入。
cos(x) 返回数的余弦。
exp(x) 返回 e 的指数。
floor(x) 对数进行下舍入。
log(x) 返回数的自然对数(底为e)。
max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
round(x) 把数4舍5入为最接近的整数。
sin(x) 返回数的正弦。
sqrt(x) 返回数的平方根。
tan(x) 返回角的正切。
toSource() 返回该对象的源代码。
valueOf() 返回 Math 对象的原始值。


Related articles: