<< Chapter < Page Chapter >> Page >

An exercise involving multiplication

Please copy the code shown in Listing 2 into an html file and open it in your browser.

Listing 2 . An exercise involving multiplication.
<!-- File JavaScript02.html --><html><body><script language="JavaScript1.3">//Compute and display the product of three // numbers, each having a different number// of significant figures. var a = 169.01var b = 0.00356 var c = 386.253var product = a * b * c document.write("product = " + product + "</br>") //Round the product to the correct number// of significant figures var rounded = product.toPrecision(5)document.write("rounded = " + rounded + "</br>") //Display a final line as a hedge against// unidentified coding errors. document.write("The End")</script></body></html>

The screen output

When you open your html file in your browser, the text shown in Figure 3 should appear in your browser window.

Figure 3 . Screen output from Listing #2.
product = 232.39900552679998 rounded = 232.40The End

The code in Listing 2 begins by declaring three variables named a , b , and c , multiplying them together, and displaying the product in the browser window. Each of the factors in theproduct have a different number of significant figures, with the factor of value 169.01 having the least number (5) of significant figures. We knowfrom rule #2 , therefore, that we need to present the result rounded to five significant figures.

The toPrecision method

Listing 2 calls a method named toPrecision on the variable named product , passing the desired number of significant figures (5) as a parameter. The method rounds the value stored in product to the desired number of digits and returns the result, which is stored in the variable named rounded . Then the contents of the variable named rounded are displayed, producing the second line of text in Figure 3 .

What about other parameter values

Note that the method named toPrecision knows nothing about significant figures. It was up to me to figure out the desired number ofsignificant figures in advance and to pass that value as a parameter to the method.

Although this has nothing to do with significant figures, it may be instructive to examine the behavior of the method named toPrecision for several different parameter values.

Figure 4 shows the result of replacing the parameter value of 5 in the call to the toPrecision method with the values in the first column of Figure 4 and displaying the value returned by the method.

Figure 4 . Behavior of the toPrecision method.
1 rounded = 2e+2 2 rounded = 2.3e+23 rounded = 232 4 rounded = 232.45 rounded = 232.40 6 rounded = 232.3997 rounded = 232.3990 10 rounded = 232.399005515 rounded = 232.399005526800 20 rounded = 232.39900552679998214

And the point is...

The point to this is to emphasize that the method named toPrecision is not a method that knows how to compute and display the required number of significant figures. Instead, according to theJavaScript documentation:

"The toPrecision() method formats a number to a specified length. A decimal point and nulls are added (if needed), to create the specified length."

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask