The Math object holds a set of constants and methods
enabling more complex mathematical operations than the basic arithmetic
operators
var root = Math.sqrt(10);
var root = Math.sqrt(10);
Property
|
Description
|
---|---|
Math.E
|
The base of the natural logarithm (Euler's constant e)
|
Math.LN2
|
Natural log of 2
|
Math.LN10
|
Natural log of 10
|
Math.LOG2E
|
Log (base 2) of e
|
Math.LOG10E
|
Log (base 10) of e
|
Math.PI
|
Pi (p)
|
Math.SQRT1_2
|
Square root of 0.5 (equivalently, one over the square root
of 2)
|
Math.SQRT2
|
Square root of 2
|
Method
|
Returns
|
---|---|
Math.abs(arg)
|
Absolute value of arg
|
Math.acos(arg)
|
Arc cosine of arg
|
Math.asin(arg)
|
Arc sine of arg
|
Math.atan(arg)
|
Arc tangent of arg
|
Math.atan2(y, x)
|
Angle between the x axis and the point (x, y), measured counterclockwise (like
polar coordinates). Note how y is passed as the first
argument rather than the second.
|
Math.ceil(arg)
|
Ceiling of arg (smallest integer
greater than or equal to arg)
|
Math.cos(arg)
|
Cosine of arg
|
Math.exp(arg)
|
e to arg
power
|
Math.floor(arg)
|
Floor of arg (greatest integer less
than or equal to arg)
|
Math.log(arg)
|
Natural log of arg (log base e of arg)
|
Math.max(arg1, arg2)
|
The greater of arg1 or arg2
|
Math.min(arg1, arg2)
|
The lesser of arg1 or arg2
|
Math.pow(arg1, arg2)
|
arg1 to the arg2
power
|
Math.random()
|
A random number in the interval [0,1]
|
Math.round(arg)
|
The result of rounding arg to the
nearest integer. If the decimal portion of arg is greater
than or equal to .5, it is rounded up. Otherwise, arg is
rounded down.
|
Math.sin(arg)
|
Sine of arg
|
Math.sqrt(arg)
|
Square root of arg
|
Math.tan(arg)
|
Tangent of arg
|
There are several aspects of the Math object that need
to be kept in mind. The trigonometric methods work in radians, so you need to
multiply any degree measurements by p / 180 before
using them. Also, because of the imprecise characteristic of floating-point
operations, you might notice minor deviations from the results you expect. For
example, though the sine of p is 0,
the following code:
alert(Math.sin(Math.PI));