9.5 The Math ObjectThe Math object allows you to work with more advanced arithmetic calculations, such as square root, trigonometric functions, logarithms, and random numbers , than are provided by the basic numeric operators. If you are doing simple calculations, you really won't need it. Unlike other objects, you don't have to create an instance of the Math object with the new keyword. It is a built-in object and has a number of properties (see Table 9.5) and methods (see Table 9.6). The Math object always starts with an uppercase M. Table 9.5. Math object properties.
Table 9.6. Math object methods.
Square Root, Power of, and PiThe Math object comes with a number of common mathematical constants (all uppercase), such as PI and natural log values, as well as methods to find the square root of a number, the power of a number, and so on. Example 9.16 demonstrates how to use some of these properties; the output is shown in Figure 9.17. Example 9.16<html> <head><title>The Math Object</title></head> <body> <h2>Math object Methods--sqrt(),pow()<br> Math object Property--PI</h2> <P> <script language="JavaScript"> 1 var num=16; document.write("<h3>The square root of " +num+ " is "); 2 document.write( Math.sqrt(num) ,".<br>"); 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 )); document.write(".</h3></font>"); </script> </body></html> Figure 9.17. Output from Example 9.16.
9.5.1 Rounding Up and Rounding DownThere are three Math methods available for rounding numbers up or down. They are the ceil(), floor() , and round() methods (see Table 9.7 for examples). The differences between the methods might be confusing because all three methods truncate the numbers after the decimal point and return a whole number. If you recall, JavaScript also provides the parseInt() function, but this function truncates the number after the decimal point, without rounding either up or down. The ceil() MethodThe ceil() method rounds a number up to the next largest whole number and then removes any numbers after the decimal point; thus, 5.02 becomes 6 because 6 is the next largest number, and “5.02 becomes “5 because “5 is larger than “6. The floor() MethodThe floor() method rounds a number down to the next lowest whole number and then removes any numbers after the decimal point; thus, 5.02 now becomes 5, and “5.02 becomes “6. The round() MethodThe round() method rounds up only if the decimal part of the number is .5 or greater. Otherwise, it rounds down to the nearest integer; thus, 5.5 is rounded up to 6, and 5.4 is rounded down to 5. Table 9.7. Rounding up and down.
Example 9.17<html> <head><title>The Math Object</title></head> <body> <h2>Rounding Numbers</h2> <p> <h3> <script language="JavaScript"> 1 var num=16.3; document.write("<I>The number being manipulated is: ", num, "</I><br><br>"); 2 document.write("The <I>Math.floor</I> method rounds down: " + Math.floor(num) + "<br>"); 3 document.write("The <I>Math.ceil</I> method rounds up: " + Math.ceil(num) +"<br>"); 4 document.write("The <I>Math.round</I> method rounds to\ the nearest integer: " + Math.round(num) + "<br>"); </script> </h3> </body> </html> Figure 9.18. Output from Example 9.17.
9.5.2 Generating Random NumbersRandom numbers are frequently used in JavaScript programs to produce random images (such as banners streaming across a screen), random messages, or random numbers (such as for lotteries or card games ). There are examples throughout this text where random numbers are used. The Math object's random() method returns a random fractional number between 0 and 1 and is seeded with the computer's system time. (The seed is the starting number for the algorithm that produces the random number.) The Math object's floor() method truncates numbers after the decimal point and returns an integer. Example 9.18<html><head><title>Random Numbers</title> <font size="+1"> <script language="JavaScript"> 1 var n = 10; 2 for(i=0; i < 10;i++){ // Generate random numbers between 0 and 10 3 document.write( Math.floor(Math.random()* (n + 1)) + "<br>"); } </script> </head> <body></body> </html> EXPLANATION
|