JavaScript advanced programming Reading Notes (12) js built-in object Math

Source: Internet
Author: User
Tags natural logarithm

Attributes of the Math object

E: Value e, bottom of the natural logarithm
LN10: the natural logarithm of 10
LN2: natural logarithm of 2
LOG2E: Base-2 logarithm of E
LOG10E: Base-10 logarithm of E
PI: Value Delivery
SQRT1_2: the square root of 1/2
SQRT2: square root of 2
Math object method: maximum value and minimum value
Min () & max () is used to obtain the minimum and maximum values of a group of numbers.

Example:

Copy codeThe 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


Approx. Pair Value

Abs () is used to return the absolute value of a number.
Example:
Copy codeThe Code is as follows:
Var iNegOne = Math. abs (-1 );
Alert (iNegOne); // oupputs 1
Var iPosOne = Math. abs (1 );
Alert (iPosOne); // outputs 1


Round decimal places to Integers

Ceil () is the rounded up function, which always rounds the number up to the nearest value.
Floor () is a downward rounding function, which always places numbers down to the nearest value.
Round () is rounded to the rounding method.
Example:
Copy codeThe 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


Exponential computing

Exp () is used to raise Math. eto the specified power.
Log () is used to return the natural logarithm of a specific number.
Pow () is used to raise a specified number to a specified power.
Sqrt () is used to return the square root of a specified number.

Trigonometric function method

Acos (x) is used to return the arc cosine of x.
Asin (x) is used to return the arc sine of x.
Atan (x) returns the arc tangent of x.
Atan2 (y, x) is used to return the arc cosine of y/x.
Cos (x) is used to return the cosine of x.
Sin (x) is used to return the sine of x.
Tan (x) returns the tangent of x.

Random Number Function

Random () is used to return a random number ranging from 0 to 1, excluding 0 and 1.
Select a random number within a range:

Copy codeThe 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.