Math
object's PI
property has the value of pi.
Core object. | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
Math
object is a top-level, predefined JavaScript object. You can automatically access it without using a constructor or calling a method.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 of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getEuler() {
return Math.E
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getNatLog10() {
return Math.LN10
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getNatLog2() {
return Math.LN2
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
E
:function getLog10e() {
return Math.LOG10E
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getLog2e() {
return Math.LOG2E
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getPi() {
return Math.PI
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getRoot1_2() {
return Math.SQRT1_2
}
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.
Property of |
Math
|
Static, Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
function getRoot2() {
return Math.SQRT2
}
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.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
abs(x)
x | A number |
x
:function getAbs(x) {
return Math.abs(x)
}
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.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
acos(x)
x | A number |
acos
method returns a numeric value between 0 and pi radians. If the value of number
is outside this range, it returns 0.
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.
x
:function getAcos(x) {If you pass -1 to
return Math.acos(x)
}
getAcos
, it returns 3.141592653589793; if you pass 2, it returns 0 because 2 is out of range.Math.asin
, Math.atan
, Math.atan2
, Math.cos
, Math.sin
, Math.tan
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
asin(x)
x | A number |
asin
method returns a numeric value between -pi/2 and pi/2 radians. If the value of number
is outside this range, it returns 0.
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.
x
:function getAsin(x) {If you pass
return Math.asin(x)
}
getAsin
the value 1, it returns 1.570796326794897 (pi/2); if you pass it the value 2, it returns 0 because 2 is out of range.Math.acos
, Math.atan
, Math.atan2
, Math.cos
, Math.sin
, Math.tan
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
atan(x)
x | A number |
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.
x
:function getAtan(x) {If you pass
return Math.atan(x)
}
getAtan
the value 1, it returns 0.7853981633974483; if you pass it the value .5, it returns 0.4636476090008061.Math.acos
, Math.asin
, Math.atan2
, Math.cos
, Math.sin
, Math.tan
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
atan2(y, x)
y, x | Number |
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.
function getAtan2(x,y) {If you pass
return Math.atan2(x,y)
}
getAtan2
the values (90,15), it returns 1.4056476493802699; if you pass it the values (15,90), it returns 0.16514867741462683.Math.acos
, Math.asin
, Math.atan
, Math.cos
, Math.sin
, Math.tan
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
ceil(x)
x | A number |
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.x
:function getCeil(x) {If you pass 45.95 to
return Math.ceil(x)
}
getCeil
, it returns 46; if you pass -45.95, it returns -45.Math.floor
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
cos(x)
x | A number |
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.
x
:function getCos(x) {If
return Math.cos(x)
}
x
equals Math.PI/2
, getCos
returns 6.123031769111886e-017; if x
equals Math.PI
, getCos
returns -1.Math.acos
, Math.asin
, Math.atan
, Math.atan2
, Math.sin
, Math.tan
x
is the argument, and E
is Euler's constant, the base of the natural logarithms.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
exp(x)
x | A number |
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.x
:function getExp(x) {If you pass
return Math.exp(x)
}
getExp
the value 1, it returns 2.718281828459045.Math.E
, Math.log
, Math.pow
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
floor(x)
x | A number |
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.x
:function getFloor(x) {If you pass 45.95 to
return Math.floor(x)
}
getFloor
, it returns 45; if you pass -45.95, it returns -46.Math.ceil
E
) of a number.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
log(x)
x | A number |
number
is outside the suggested range, the return value is always -1.797693134862316e+308.
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.
x
:function getLog(x) {If you pass
return Math.log(x)
}
getLog
the value 10, it returns 2.302585092994046; if you pass it the value 0, it returns -1.797693134862316e+308 because 0 is out of range.Math.exp
, Math.pow
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
max(x,y)
x, y | Numbers. |
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.x
and y
:function getMax(x,y) {If you pass
return Math.max(x,y)
}
getMax
the values 10 and 20, it returns 20; if you pass it the values -10 and -20, it returns -10.Math.min
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
min(x,y)
x, y | Numbers. |
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.x
and y
:function getMin(x,y) {If you pass
return Math.min(x,y)
}
getMin
the values 10 and 20, it returns 10; if you pass it the values -10 and -20, it returns -20.Math.max
base
to the exponent
power, that is, base
exponent.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
pow(x,y)
base | The base number |
exponent |
The exponent to which to raise base
|
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.function raisePower(x,y) {If
return Math.pow(x,y)
}
x
is 7 and y
is 2, raisePower
returns 49 (7 to the power of 2).Math.exp
, Math.log
Method of |
Math
|
Static | |
Implemented in |
Navigator 2.0, LiveWire 1.0: Unix only Navigator 3.0, LiveWire 1.0: all platforms |
random()
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.//Returns a random number between 0 and 1
function getRandom() {
return Math.random()
}
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
round(x)
x | A number |
number
is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number
is less than .5, the argument is rounded to the next lowest 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.
//Displays the value 20
document.write("The rounded value is " + Math.round(20.49))
//Displays the value 21
document.write("<P>The rounded value is " + Math.round(20.5))
//Displays the value -20
document.write("<P>The rounded value is " + Math.round(-20.5))
//Displays the value -21In server-side JavaScript, you can display the same output by calling the
document.write("<P>The rounded value is " + Math.round(-20.51))
write
function instead of using document.write
.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
sin(x)
x | A number |
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.
x
:function getSine(x) {If you pass
return Math.sin(x)
}
getSine
the value Math.PI/2
, it returns 1.Math.acos
, Math.asin
, Math.atan
, Math.atan2
, Math.cos
, Math.tan
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
sqrt(x)
x | A number |
number
is outside the required range, sqrt
returns 0.
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.
x
:function getRoot(x) {If you pass
return Math.sqrt(x)
}
getRoot
the value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095.
Method of |
Math
|
Static | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
tan(x)
x | A number |
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.
x
:function getTan(x) {If you pass
return Math.tan(x)
}
Math.PI/4
to getTan
, it returns 0.9999999999999999.Math.acos
, Math.asin
, Math.atan
, Math.atan2
, Math.cos
, Math.sin
Last Updated: 10/31/97 16:00:33