JavaScript Instance Tutorial (16) using JavaScript for mathematical functions

Source: Internet
Author: User
Tags constant cos functions integer natural logarithm pow square root version
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 Article

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.