HTML and CSS Reference
In-Depth Information
Table 9.6
Math
Object Methods (continued)
Method
Functionality
Math.tan(Number)
Tangent of
Number
in radians
Math.toString(Number)
Converts
Number
to string
* Returns the value of
E
x
where
E
is Euler's constant and
x
is the argument passed to it. Euler's constant is
approximately 2.7183.
Square Root, Power of, and Pi.
The
Math
object comes with a number of common
mathematical constants (all uppercase), such as
PI
and natural log values, as well as meth-
ods to find the square root of a number, the power of a number, and so on. Example 9.20
demonstrates how to use some of these properties; the output is shown in Figure 9.24.
EXAMPLE
9.20
<html>
<head><title>The Math Object</title></head>
<body>
<h2>Math object Methods--sqrt(),pow()<br />
Math object Property--PI
</h2>
<h3>
<script type="text/javascript">
1
var num=16;
document.write("The square root of " +num+ " is ");
document.write(
Math.sqrt(num)
,".<br />");
2
document.write("PI is ");
3
document.write(
Math.PI
);
document.write(".<br />"+num+" raised to the 3rd power is " );
4
document.write(
Math.pow(num,3)
+ ".");
</script>
</h3>
</body>
</html>
EXPLANATION
1
The number 16 will be manipulated by
Math
methods and constants.
2
The
Math
object's
sqrt()
method returns the square root of number 16, which is 4.
3
The
Math
object's PI constant produces the value of PI.
4
The
Math
object's
pow()
methods raises and returns number 16 to the 3rd power,
which is 4096 (see Figure 9.24).