Mathematical functions of JavaScript

Source: Internet
Author: User
Tags natural logarithm
In JavaScript, mathematical methods can be divided into the following types: constans (constants), power functions (multiplication functions), trigonometic funtic (trigonometric functions), and rounding functions (rounding functions) and random numbers (random number ). The following are explained one by one:

Constants and multiplication Functions

Math. e

Base of natural logarithm (constant)

2.718

Math. ln10

Natural logarithm of 10

2.302

Math. ln2

Natural logarithm of 2

0.693

Math. Pi

Circumference Rate

3.1415

Math. sqrt1_2

Square root of 1/2

0.707

Math. sqrt2

Square root of 2

1.414

Math. SQRT (X)

Square root of X

It depends on the value of X.

Math. Pow (x, n)

Returns the nth power of X.

The parameters are X and N.

Math. Log (N)

Returns the natural logarithm of N.

The parameter is N.

Math. exp (N)

Returns the nth power of E.

The parameter is N.

Trigonometric function

math. Cos (x)

cosine function of x

math. Sin (x)

sine function of x

math. Tan (x)

tangent function of x

math. ACOs (y)

returns the arc cosine of x

math. asin (y)

arcsin function of x

math. atan (y)

arctangent function of x

Note: The unit of the return values of parameter X and inverse functions is radian (for example, PI radian = 180 degrees)
Rounding functions and random numbers

Math. Random ()

Generate a random number from 0 to 1

Math. Round (X)

Returns the value closest to integer x.

Math. Floor (X)

Returns the value closest to integer x and smaller than X.

Math. Ceil (X)

Returns the value closest to integer x and greater than X.

Math. Min (A, B, C)

Returns the smallest value in the parameter list.

Math. Max (A, B, C)

Returns the maximum value in the parameter list.

Note: The math. Random () function can only be executed in navigator 2.0 of the UNIX version.

The most common of these functions is to generate a random number between two given values. The following function is a good example:

Function randomvalue (low, high ){

Alert (math. Floor (math. Random () * (1 + high-low) + low ));

}

In addition, you canCodeTo avoid repeated use of math identifiers, for example, the following code:

Function randomvalue (low, high ){

With (math ){

Alert (floor (random () * (1 + high-low) + low ));

}

}

Remember that the math. Random () function can only be executed in navigator 2.0 of the UNIX version, but not in the browser of the Windows version. Therefore, this function is generally not used.

The following describes a more complex function. This function returns the length of the third side of the triangle. The condition is the angle between the two sides and the two sides of the triangle. The Code is as follows:

Function findside (sidea, sideb, angle ){

With (math ){

VaR TMP = POW (sidea, 2) + POW (sideb, 2)-2 * sidea * sideb * Cos (angle );

Alert ("side length is" + SQRT (TMP ));

}

}

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.