Home > Web Front-end > JS Tutorial > body text

Talk about Math_javascript skills of JavaScript type system

WBOY
Release: 2016-05-16 15:21:21
Original
1091 people have browsed it

A must read when opening the door

Math is different from other objects. 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

Attributes

Math.E is the base of the natural logarithm, which is the value of the constant e (approximately equal to 2.718)
Math.PI pie value (approximately equal to 3.14159)
console.log(Math.E);//2.718281828459045
console.log(Math.PI);//3.141592653589793
Math.LN2 Natural logarithm of 2 (approximately equal to 0.693)
Math.LN10 The natural logarithm of 10 (approximately equal to 2.302)
Math.LOG2E The logarithm of e with base 2 (approximately equal to 1.414)
Math.LOG10E The logarithm of e with base 10 (approximately equal to 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
Math.SQRT2 The square root of 2 (approximately equal to 1.414)
Math.SQRT1_2 The square root of 1/2, which is the reciprocal of the square root of 2 (approximately equal to 0.707)
console.log(Math.SQRT2);//1.4142135623730951
console.log(Math.SQRT1_2);//0.7071067811865476

Method

These methods all involve Number() implicit type conversion; if it exceeds the scope of the method, NaN will be returned

Math.min() returns the minimum value in a set of numbers
Math.max() returns the maximum value in a set of numbers
console.log(Math.min(1,2,3));//1
console.log(Math.max(1,2,3));//3
Math.ceil(num) rounds up to an integer
Math.floor(num) rounds down to an integer
Math.round(num) rounds to 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 a 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 Math.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 num raised to the power
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 value of x
Math.cos(x) returns the cosine of x
Math.tan(x) returns the tangent of x
Math.asin(x) returns the arcsine of x (x must be a number between -1 and 1)
Math.acos(x) returns the inverse cosine of x (x must be a number between -1 and 1)
Math.atan(x) returns the arctangent of x
Math.atan2(y,x) returns the arctangent 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 
Copy after login

[tips2] Randomly select a value from a certain integer range

value = Math.floor(Math.random()*可能值的总数 + 第一个可能的值)
Copy after login

[tips3] Randomly select a value from 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); 
Copy after login

Math object method

Method Description
abs(x) Returns the absolute value of the number.
acos(x) Returns the arc cosine of a number.
asin(x) Returns the arcsine of the number.
atan(x) Returns the arctangent of x as a number between -PI/2 and PI/2 radians.
atan2(y,x) Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 radians).
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) 把數四捨五入為最接近的整數。
sin(x) 傳回數的正弦。
sqrt(x) 傳回數的平方根。
tan(x) 返回角落的正切。
toSource() 傳回該物件的原始碼。
valueOf() 傳回 Math 物件的原始值。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!