| Previous Contents Index Next |
| Core JavaScript Reference 1.5 |
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.
Created by
The Math object is a top-level, predefined JavaScript object. You can automatically access it without using a constructor or calling a method.
Description
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)
}
Property
Description
E
Euler's constant and the base of natural logarithms, approximately 2.718.
LN2
LN10
LOG2E
LOG10E
PI
Ratio of the circumference of a circle to its diameter, approximately 3.14159.
SQRT1_2
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
SQRT2
Method
Description
abs
acos
asin
atan
atan2
ceil
Returns the smallest integer greater than or equal to a number.
cos
exp
Returns Enumber, where number is the argument, and E is Euler's constant, the base of the natural logarithms.
floor
log
max
min
pow
random
round
Returns the value of a number rounded to the nearest integer.
sin
sqrt
tan
In addition, this object inherits the watch and unwatch methods from Object.
Returns the absolute value of a number.
x
Examples
The following function returns the absolute value of the variable x:function getAbs(x) {
return Math.abs(x)
}
Description
Because abs is a static method of Math, you always use it as Math.abs(), rather than as a method of a Math object you created.
Returns the arccosine (in radians) of a number.
x
Description
The acos method returns a numeric value between 0 and pi radians. If the value of number is outside this range, it returns NaN.Because acos is a static method of Math, you always use it as Math.acos(), rather than as a method of a Math object you created.
Examples
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.
See also
Math.asin, Math.atan, Math.atan2, Math.cos, Math.sin, Math.tan
Returns the arcsine (in radians) of a number.
x
Description
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.Because asin is a static method of Math, you always use it as Math.asin(), rather than as a method of a Math object you created.
Examples
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.
See also
Math.acos, Math.atan, Math.atan2, Math.cos, Math.sin, Math.tan
Returns the arctangent (in radians) of a number.
x
Description
The atan method returns a numeric value between -pi/2 and pi/2 radians.Because atan is a static method of Math, you always use it as Math.atan(), rather than as a method of a Math object you created.
Examples
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.
See also
Math.acos, Math.asin, Math.atan2, Math.cos, Math.sin, Math.tan
Returns the arctangent of the quotient of its arguments.
y, x
Description
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.
Examples
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.
See also
Math.acos, Math.asin, Math.atan, Math.cos, Math.sin, Math.tan
Returns the smallest integer greater than or equal to a number.
x
Description
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.
Examples
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.
See also
Math.floor
Returns the cosine of a number.
x
Description
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.
Examples
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.
See also
Math.acos, Math.asin, Math.atan, Math.atan2, Math.sin, Math.tan
Euler's constant and the base of natural logarithms, approximately 2.718.
Description
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.
Examples
The following function returns Euler's constant:function getEuler() {
return Math.E
}
Returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms.
x
Description
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.
Examples
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.
See also
Math.E, Math.log, Math.pow
Returns the largest integer less than or equal to a number.
x
Description
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.
Examples
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.
See also
Math.ceil
The natural logarithm of 2, approximately 0.693.
Examples
The following function returns the natural log of 2:function getNatLog2() {
return Math.LN2
}
Description
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.
The natural logarithm of 10, approximately 2.302.
Examples
The following function returns the natural log of 10:function getNatLog10() {
return Math.LN10
}
Description
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.
Returns the natural logarithm (base E) of a number.
x
Description
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.
Examples
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.
The base 2 logarithm of E (approximately 1.442).
Examples
The following function returns the base 2 logarithm of E:function getLog2e() {
return Math.LOG2E
}
Description
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.
The base 10 logarithm of E (approximately 0.434).