Reading Notes for JavaScript advanced programming (12): built-in object math

Source: Internet
Author: User
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:

 
VaRIMAX = math. Max (1, 2, 3 );
Alert (IMAX );//Outputs 3
VaRImin = math. Min (1, 2, 3 );
Alert (Imin );//Outputs 1
Approx. Pair Value

ABS () is used to return the absolute value of a number.

Example:

VaRInegone = math. Abs (-1 );
Alert (inegone );//Oupputs 1
VaRIposone = 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:

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, including 0 but not 1.

Select a random number within a range:

 
FunctionSelectfrom (ifirstvalue, ilastvalue ){
VaRIchoices = ilastvalue-ifirstvalue + 1;
ReturnMath. Floor (math. Random () * ichoices + ifirstvalue );
}

//Demo
VaRInum = selectfrom (2, 10 );

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.