本文档基于 CC协议(Creative Commons Share-Alike License)发布,是developer.mozilla.com, www.mozref.comwww.aptana.com共同劳动的成果。

Math : Object

A built-in object that has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.

Browser/User Agent Support

IEMozillaNetscapeOperaSafari
3.0+1.0+2.0+7.0+1.0+

Properties

PropertyIEMozillaNetscapeOperaSafari
Euler's constant and the base of natural logarithms, approximately 2.718.
3.0+1.0+2.0+7.0+1.0+
 
The natural logarithm of 10, approximately 2.302.
3.0+1.0+2.0+7.0+1.0+
 
The natural logarithm of 2, approximately 0.693.
3.0+1.0+2.0+7.0+1.0+
 
The base 10 logarithm of E (approximately 0.434).
3.0+1.0+2.0+7.0+1.0+
 
The base 2 logarithm of E (approximately 1.442).
3.0+1.0+2.0+7.0+1.0+
 
The ratio of the circumference of a circle to its diameter, approximately 3.14159.
3.0+1.0+2.0+7.0+1.0+
 
The square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
3.0+1.0+2.0+7.0+1.0+
 
The square root of 2, approximately 1.414.
3.0+1.0+2.0+7.0+1.0+
 

Methods

MethodIEMozillaNetscapeOperaSafari
Returns the absolute value of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the arccosine (in radians) of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the arcsine (in radians) of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the arctangent (in radians) of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the arctangent of the quotient of its arguments.
3.0+1.0+2.0+7.0+1.0+
 
Returns the smallest integer greater than or equal to a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the cosine of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms.
3.0+1.0+2.0+7.0+1.0+
 
Returns the largest integer less than or equal to a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the natural logarithm (base E) of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the larger of two numbers.
3.0+1.0+2.0+7.0+1.0+
 
Returns the smaller of two numbers.
3.0+1.0+2.0+7.0+1.0+
 
Returns base to the exponent power, that is, baseexponent.
3.0+1.0+2.0+7.0+1.0+
 
Returns a pseudo-random number in the range [0,1) -- that is, between 0 (inclusive) and 1 (exclusive). The random number generator is seeded from the current time, as in Java.
3.0+1.0+3.0+7.0+1.0+
 
Returns the value of a number rounded to the nearest integer.
3.0+1.0+2.0+7.0+1.0+
 
Returns the sine of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the square root of a number.
3.0+1.0+2.0+7.0+1.0+
 
Returns the tangent of a number.
3.0+1.0+2.0+7.0+1.0+
 

Remarks

All properties and methods of Math are static. You refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument. Constants are defined with the full precision of real numbers in JavaScript.

It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,

with (Math) {
   a = PI * r*r
   y = r*sin(theta)
   x = r*cos(theta)
}

References

Number

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

Property Detail

static Number E

Euler's constant and the base of natural logarithms, approximately 2.718.

Using Math.E

The following function returns Euler's constant:

function getEuler() {
   return Math.E
}
Remarks

Because E is a static property of Math, you always use it as Math.E, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number LN10

The natural logarithm of 10, approximately 2.302.

Using Math.LN10

The following function returns the natural log of 10:

function getNatLog10() {
   return Math.LN10
}
Remarks

Because LN10 is a static property of Math, you always use it as Math.LN10, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number LN2

The natural logarithm of 2, approximately 0.693.

Using Math.LN2

The following function returns the natural log of 2:

function getNatLog2() {
   return Math.LN2
}
Remarks

Because LN2 is a static property of Math, you always use it as Math.LN2, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number LOG10E

The base 10 logarithm of E (approximately 0.434).

Using Math.LOG10E

The following function returns the base 10 logarithm of E:

function getLog10e() {
   return Math.LOG10E
} 
Remarks

Because LOG10E is a static property of Math, you always use it as Math.LOG10E, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number LOG2E

The base 2 logarithm of E (approximately 1.442).

Using Math.LOG2E

The following function returns the base 2 logarithm of E:

function getLog2e() {
   return Math.LOG2E
}
Remarks

Because LOG2E is a static property of Math, you always use it as Math.LOG2E, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number PI

The ratio of the circumference of a circle to its diameter, approximately 3.14159.

Using PI

The following function returns the value of pi:

function getPi() {
   return Math.PI
}
Remarks

Because PI is a static property of Math, you always use it as Math.PI, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number SQRT1_2

The square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.

Using SQRT1_2

The following function returns 1 over the square root of 2:

function getRoot1_2() {
   return Math.SQRT1_2
}
Remarks

Because SQRT1_2 is a static property of Math, you always use it as Math.SQRT1_2, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

static Number SQRT2

The square root of 2, approximately 1.414.

Using Math.SQRT2

The following function returns the square root of 2:

function getRoot2() {
   return Math.SQRT2
}
Remarks

Because SQRT2 is a static property of Math, you always use it as Math.SQRT2, rather than as a property of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

Method Detail

abs(Number x) : static Number

Returns the absolute value of a number.

NumberxA number.

Using Math.abs

The following function returns the absolute value of the variable x:

function getAbs(x) {
   return Math.abs(x)
}
Remarks

abs is a static method of Math, so you always use it as Math.abs(), rather than as a method of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

acos(Number x) : static Number

Returns the arccosine (in radians) of a number.

NumberxA number.

Using Math.acos

The following function returns the arccosine of the variable x:

function getAcos(x) {
   return Math.acos(x)
}

If you pass -1 to getAcos, it returns 3.141592653589793; if you pass 2, it returns NaN because 2 is out of range.

Remarks

The acos method returns a numeric value between 0 and pi radians for x between -1 and 1. If the value of number is outside this range, it returns NaN.

acos is a static method of Math, so you always use it as Math.acos(), rather than as a method of a Math object you created.

See Also

asin | atan | atan2 | cos | sin | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

asin(Number x) : static Number

Returns the arcsine (in radians) of a number.

NumberxA number.

Using Math.asin

The following function returns the arcsine of the variable x:

function getAsin(x) {
   return Math.asin(x)
}

If you pass getAsin the value 1, it returns 1.570796326794897 (pi/2); if you pass it the value 2, it returns NaN because 2 is out of range.

Remarks

The asin method returns a numeric value between -pi/2 and pi/2 radians. If the value of number is outside this range, it returns NaN.

asin is a static method of Math, so you always use it as Math.asin(), rather than as a method of a Math object you created.

See Also

acos | atan | atan2 | cos | sin | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

atan(Number x) : static Number

Returns the arctangent (in radians) of a number.

NumberxA number.

Using Math.atan

The following function returns the arctangent of the variable x:

function getAtan(x) {
   return Math.atan(x)
}

If you pass getAtan the value 1, it returns 0.7853981633974483; if you pass it the value .5, it returns 0.4636476090008061.

Remarks

The atan method returns a numeric value between -pi/2 and pi/2 radians.

atan is a static method of Math, so you always use it as Math.atan(), rather than as a method of a Math object you created.

See Also

acos | asin | atan2 | cos | sin | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

atan2(Object y, Object x) : static Number

Returns the arctangent of the quotient of its arguments.

ObjectyNumber.
ObjectxNumber.

Using Math.atan2

The following function returns the angle of the polar coordinate:

function getAtan2(x,y) {
   return Math.atan2(x,y)
}

If you pass getAtan2 the values (90,15), it returns 1.4056476493802699; if you pass it the values (15,90), it returns 0.16514867741462683.

Remarks

The atan2 method returns a numeric value between -pi and pi representing the angle theta of an (x,y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x,y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.

atan2 is passed separate x and y arguments, and atan is passed the ratio of those two arguments.

Because atan2 is a static method of Math, you always use it as Math.atan2(), rather than as a method of a Math object you created.

See Also

acos | asin | atan | cos | sin | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

ceil(Number x) : static Number

Returns the smallest integer greater than or equal to a number.

NumberxA number.

Using Math.ceil

The following function returns the ceil value of the variable x:

function getCeil(x) {
   return Math.ceil(x)
}

If you pass 45.95 to getCeil, it returns 46; if you pass -45.95, it returns -45.

Remarks

Because ceil is a static method of Math, you always use it as Math.ceil(), rather than as a method of a Math object you created.

See Also

floor

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

cos(Number x) : static Number

Returns the cosine of a number.

NumberxA number.

Using Math.cos

The following function returns the cosine of the variable x:

function getCos(x) {
   return Math.cos(x)
}

If x equals 2*Math.PI, getCos returns 1; if x equals Math.PI, the getCos method returns -1.

Remarks

The cos method returns a numeric value between -1 and 1, which represents the cosine of the angle.

Because cos is a static method of Math, you always use it as Math.cos(), rather than as a method of a Math object you created.

See Also

acos | asin | atan | atan2 | sin | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

exp(Number x) : static Number

Returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms.

NumberxA number.

Using Math.exp

The following function returns the exponential value of the variable x:

function getExp(x) {
   return Math.exp(x)
}

If you pass getExp the value 1, it returns 2.718281828459045.

Remarks

Because exp is a static method of Math, you always use it as Math.exp(), rather than as a method of a Math object you created.

See Also

Math.E | log | pow

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

floor(Number x) : static Number

Returns the largest integer less than or equal to a number.

NumberxA number.

Using Math.floor

The following function returns the floor value of the variable x:

function getFloor(x) {
   return Math.floor(x)
}

If you pass 45.95 to getFloor, it returns 45; if you pass -45.95, it returns -46.

Remarks

Because floor is a static method of Math, you always use it as Math.floor(), rather than as a method of a Math object you created.

See Also

ceil

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

log(Number x) : static Number

Returns the natural logarithm (base E) of a number.

NumberxA number.

Using Math.log

The following function returns the natural log of the variable x:

function getLog(x) {
   return Math.log(x)
}

If you pass getLog the value 10, it returns 2.302585092994046; if you pass it the value 0, it returns -Infinity; if you pass it the value -1, it returns NaN because -1 is out of range.

Remarks

If the value of number is negative, the return value is always NaN.

Because log is a static method of Math, you always use it as Math.log(), rather than as a method of a Math object you created.

See Also

exp | pow

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

max(Number x, Number y) : static Number

Returns the larger of two numbers.

NumberxNumber.
NumberyNumber.

Using Math.max

The following function evaluates the variables x and y:

function getMax(x,y) {
   return Math.max(x,y)
}

If you pass getMax the values 10 and 20, it returns 20; if you pass it the values -10 and -20, it returns -10.

Remarks

Because max is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created.

See Also

min

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | enhanced in ECMAScript v3

min() : static Number

Returns the smaller of two numbers.

Using Math.min

The following function evaluates the variables x and y:

function getMin(x,y) {
   return Math.min(x,y)
}

If you pass getMin the values 10 and 20, it returns 10; if you pass it the values -10 and -20, it returns -20.

Remarks

Because min is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created.

See Also

max

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | enhanced in ECMAScript v3

pow() : static Number

Returns base to the exponent power, that is, baseexponent.

Using Math.pow

function raisePower(x,y) {
   return Math.pow(x,y)
}

If x is 7 and y is 2, raisePower returns 49 (7 to the power of 2).

Remarks

Because pow is a static method of Math, you always use it as Math.pow(), rather than as a method of a Math object you created.

See Also

exp | log

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

random() : static Number

Returns a pseudo-random number in the range [0,1) -- that is, between 0 (inclusive) and 1 (exclusive). The random number generator is seeded from the current time, as in Java.

Using Math.random

//Returns a random number between 0 (inclusive) and 1 (exclusive)
function getRandom() {
   return Math.random()
}
Remarks

Because random is a static method of Math, you always use it as Math.random(), rather than as a method of a Math object you created.

Availability

JavaScript 1.1 | JScript 1.0 | ECMAScript v1

round(Number x) : static Number

Returns the value of a number rounded to the nearest integer.

NumberxA number.

Using Math.round

//Returns the value 20
x=Math.round(20.49)

//Returns the value 21
x=Math.round(20.5)

//Returns the value -20
x=Math.round(-20.5)

//Returns the value -21
x=Math.round(-20.51)
Remarks

If the fractional portion of number is .5 or greater, the argument is rounded to the next higher integer. If the fractional portion of number is less than .5, the argument is rounded to the next lower integer.

Because round is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

sin(Number x) : static Number

Returns the sine of a number.

NumberxA number.

Using Math.sin

The following function returns the sine of the variable x:

function getSine(x) {
   return Math.sin(x)
}

If you pass getSine the value Math.PI/2, it returns 1.

Remarks

The sin method returns a numeric value between -1 and 1, which represents the sine of the argument.

Because sin is a static method of Math, you always use it as Math.sin(), rather than as a method of a Math object you created.

See Also

acos | asin | atan | atan2 | cos | tan

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

sqrt(Number x) : static Number

Returns the square root of a number.

NumberxA number.

Using Math.sqrt

The following function returns the square root of the variable x:

function getRoot(x) {
   return Math.sqrt(x)
}

If you pass getRoot the value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095.

Remarks

If the value of number is negative, sqrt returns NaN.

Because sqrt is a static method of Math, you always use it as Math.sqrt(), rather than as a method of a Math object you created.

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1

tan(Number x) : static Number

Returns the tangent of a number.

NumberxA number.

Using Math.tan

The following function returns the tangent of the variable x:

function getTan(x) {
   return Math.tan(x)
}
Remarks

The tan method returns a numeric value that represents the tangent of the angle.

Because tan is a static method of Math, you always use it as Math.tan(), rather than as a method of a Math object you created.

See Also

acos | asin | atan | atan2 | cos | sin

Availability

JavaScript 1.0 | JScript 1.0 | ECMAScript v1