<< Chapter < Page Chapter >> Page >

Examples of significant digits

Referring back to the physics textbook mentioned earlier, Figure 1 shows:

  • Four different numbers
  • The number of significant figures in each number.
  • The default JavaScript exponential representation of each number.
Figure 1 . Examples of significant figures.
1. 409.8 4 4.098e+2 2. 0.058700 5 5.87e-23. 9500 ambiguous 9.5e+3 4. 950.0 * 10^1 4 9.5e+3

Note that the default JavaScript exponential representation fails to display the significant trailing zeros for the numbers on row 2 and row 5. Iwill show you some ways that you may be able to deal with this issue later but you may not find them to be very straightforward.

Discussion and sample code

Beyond knowing about scientific notation and significant figures from a formatting viewpoint, you need to know how to perform arithmetic whilesatisfying the rules for scientific notation and significant figures.

Performing arithmetic involves three main rules :

  1. For addition and subtraction, the final result should be rounded so as to have the same number of decimal places as the number that was included inthe sum that has the smallest number of decimal places. In accordance with the discussion early in this module, this is the least precise number.
  2. For multiplication and division, the final result should be rounded to have the same number of significant figures as the number that was includedin the product with the smallest number of significant figures.
  3. The two rules listed above should not be applied to intermediate calculations. For intermediate calculations, keep as many digits aspractical. Round to the correct number of significant figures or the correct number of decimal places in the final result.

An exercise involving addition

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

Listing 1 . An exercise involving addition.
<!-- File JavaScript01.html --><html><body><script language="JavaScript1.3">//Compute and display the sum of three // numbersvar a = 169.01 var b = 0.00356var c = 385.293 var sum = a + b + cdocument.write("sum = " + sum + "<br/>") //Round the sum to the correct number// of digits to the right of the decimal // point.var round = sum.toFixed(2) document.write("round = " + round + "<br/>") //Display a final line as a hedge against// unidentified coding errors. document.write("The End")</script></body></html>

Screen output

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

Figure 2 . Screen output from Listing #1.
sum = 554.30656 round = 554.31The End

The code in Listing 1 begins by declaring three variables named a , b , and c , adding them together, and displaying the sum in the JavaScript default format in the browser window.

Too many decimal digits

As you can see from the first line in Figure 2 , the result is computed and displayed with eight decimal digits, five of which are to the right of thedecimal point. We know, however, from rule #1 , that we should present the result rounded to a precision of two digits to theright of the decimal point in order to match the least precise of the numbers included in the sum. In this case, the value stored in the variable named a is the least precise.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask