# JavaScript Instance Tutorial (16) using JavaScript for mathematical functions

Source: Internet
Author: User
javascript| Functions | Tutorials in JavaScript, mathematical methods can be divided into the following categories: Constans (constant), power functions (exponentiation function), trigonometic functions (trigonometric functions), Rounding functions (rounding function) and random numbers (random number). The following are described individually:

Constants and exponentiation functions

 Math.e The bottom of the natural logarithm (for constant) 2.718 Math.ln10 10 of the natural logarithm 2.302 Math.ln2 2 of the natural logarithm 0.693 Math.PI Pi 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 The value of the more X is related Math.pow (x, N) Returns the value of the N-second side of X Parameters are X and n Math.log (N) Returns the natural logarithm of n Parameter is n Math.exp (N) Returns the value of the N-second side of E Parameter is n

Trigonometric function

 math.cos (x) x cosine function math.sin (x) x sine function math.tan (x) x's tangent function math.acos (y) x's anti-cosine function math.asin (y) x the inverse chord function math.atan (y) x's inverse tangent function

Note here: Both the parameter X and the units of the returned values of the inverse function are radians (for example, pi radians = 180 degrees)
Rounding functions and random numbers

 Math.random () Generate random numbers from 0 to 1 Math.Round (x) Take the value closest to the integer x Math.floor (x) Take the nearest integer x and the number smaller than X Math.ceil (x) Take the nearest integer x and the number larger than X Math.min (A, B, c) Returns the smallest number in a parameter list Math.max (A, B, c) Returns the largest number in a parameter list

Note here: the function math.random () can only be performed in the UNIX version of Navigator 2.0.

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

function Randomvalue (Low, high) {

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

}

In addition, you can use the WITH statement in complex code to avoid the repetition of the math identifier, such as the following code:

function Randomvalue (Low, high) {

With (Math) {

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

}

}

Keep in mind that the math.random () function can only be performed in the UNIX version of Navigator 2.0, not in the Windows version of the browser, so this function is not used in general.

The following is a more complex function. This function returns the length of the third edge of the triangle, given a condition that is the angle between the sides of the triangle and the sides. The specific 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));

}

}

Related Keywords:
Related Article ## Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now > ## Starter Package

SSD Cloud server and data transfer for only \$2.50 a month

Get Started > ## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD