↑
Main Page
pow
Here,
Math.E
is first raised to the power of 10 by using
exp()
, and then
log()
returns 10 as the expo-
nent necessary to equal that number. If you are confused, you’re not alone. This type of stuff stumps
high school and college math students worldwide. Chances are if you don’t know what the natural loga-
rithm is, you’ll probably never need to code it.
The
pow()
method is used to raise a number to a given power, such as raising 2 to the power of 10 (rep-
resented in math as 2
10
):
var iNum = Math.pow(2, 10);
The first argument of
pow()
is the base number, in this case, 2. The second argument is the power to
raise it to, which is 10 in this example.
The last method in this group is
sqrt()
, which returns the square root of a given number. It takes only
one argument, which is the number whose square root you want to find. So to find the square root of 4,
you need only this line of code:
var iNum = Math.sqrt(4);
alert(iNum); //outputs “2”
Of course, the square root of 4 is 2, which is output in this code.
You may ask, “What does the square root have to do with exponents?” The square root of a number is
actually that number raised to the one-half power; for example, 2
1/2
is the square root of 2.
There is also a complete set of geometric methods included in the
Math
object. These are displayed in the
following table.
Method
Description
acos(
x
)
Returns the arc cosine of
x
.
asin(
x
)
Returns the arc sine of
x
.
atan(
x
)
Returns the arc tangent of
x
.
atan2(
y
,
x
)
Returns the arc cosine of
y/x
.
cos(
x
)
Returns the cosine of
x
.
sin(
x
)
Returns the sine of
x
.
tan(
x
)
Returns the tangent of
x
.
It is not recommended to use
Math.E
as a base for the
pow()
method. Always use
exp()
for this because it does special calculations to determine the value more
accurately.
86
Chapter 3
06_579088 ch03.qxd 3/28/05 11:36 AM Page 86
Free JavaScript Editor
Ajax Editor
©
→ R7