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

JavaScript Advanced Programming Reading Notes (12) js built-in object Math_javascript skills

WBOY
Release: 2016-05-16 17:51:05
Original
1188 people have browsed it

Attributes of the Math object

E: value e, the base of the natural logarithm
LN10: the natural logarithm of 10
LN2: the natural logarithm of 2
LOG2E: 2 as the base E Logarithm
LOG10E: Logarithm of E with base 10
PI: Value pie
SQRT1_2: Square root of 1/2
SQRT2: Square root of 2
Math object method: Maximum Value and minimum value
min()&&max() is used to get the minimum and maximum value in a set of numbers.

Example:

Copy code The code is as follows:

var iMax=Math.Max(1,2,3);
alert(iMax);//outputs 3
var iMin=Math.Min(1,2,3);
alert(iMin); //outputs 1


Approximate value

abs() is used to return the absolute value of a number.
Example:
Copy code The code is as follows:

var iNegOne=Math.abs( -1);
alert(iNegOne);//oupputs 1
var iPosOne=Math.abs(1);
alert(iPosOne);//outputs 1


Round decimals into integers

ceil() is an upward rounding function, always rounding numbers up to the nearest value
floor() is a downward rounding function, always Rounds the number down to the nearest value
round() is the rounding method
Example:
Copy code The code is as follows:

alert(Math.ceil(25.5));//oputpus 26
alert(Math.floor(25.5));//oputpus 25
alert(Math.round(25.5));//oputpus 26


Exponent calculation

exp() is used to raise Math.E to the specified power
log() is used to return the natural logarithm of a specific number
pow() is used to raise the specified number to the specified power
sqrt() is used to return the square root of the specified number

Trigonometric Function method

acos(x) is used to return the arc cosine value of x
asin(x) is used to return the arc sine value of x
atan(x) is used to return the arc tangent value of x
atan2(y,x) is used to return the inverse cosine value of y/x
cos(x) is used to return the cosine value of x
sin(x) is used to return the sine value of x
tan(x) is used to return the tangent value of x

Random number function

random() is used to return a random number between 0 and 1, excluding 0 and 1 Select random numbers within a certain range:


Copy code The code is as follows:
function selectFrom(iFirstValue,iLastValue){
var iChoices=iLastValue-iFirstValue 1;
return Math.floor(Math.random()*iChoices iFirstValue);
}
//demo
var iNum=selectFrom(2,10);


Author: Artwl
Source: http://artwl.cnblogs.com
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