TL16.TXT 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 74
  2. TURBO-LESSON 16: REAL NUMBERS
  3. OBJECTIVES - In this lesson, you will learn about:
  4. 1. Range of Real Numbers
  5. 2. Input/Output of Real Numbers
  6. 3. Calculations with Real Numbers
  7. 4. Calculations with Integers and Real Numbers
  8. 1. Range of Real Numbers
  9. For business processing (dollar amounts), and scientific
  10. processing, integers alone are not adequate. Decimal numbers and
  11. sometimes numbers in scientific notation are needed.
  12. TURBO provides Real Numbers with 11 significant digits of
  13. precision in the range:
  14. 1E-38 to 1E+38
  15. (The BCD version of TURBO provides 18 significant digits and a
  16. range of 1E-63 to 1E+63, but this set of TURBO-LESSONS deals
  17. with the more limited range above.)
  18. ##### DO:
  19. Run PROG16.
  20. Enter 444.333222111 and examine the result presented in
  21. scientific notation.
  22. How many digits are retained before the E? (I counted 11,
  23. including digits on both sides of the decimal point.)
  24. This is the 11 significant digits of precision. Note that the
  25. last 1 you entered was dropped.
  26. Œ
  27. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 75
  28. 2. Input/Output of Real Numbers.
  29. You will need to know what to expect with various combinations of
  30. input and output of real numbers.
  31. ##### DO:
  32. Run PROG16.
  33. Enter 444.333222111 and study the various outputs.
  34. When the real number, A, is output without any formatting,
  35. scientific notation is used:
  36. 4.4433322211 are the significant digits.
  37. E+02 means multiply by 10 to the 2nd power to get the number.
  38. If you want the number presented in some other form, you can add
  39. the :w:d formatting to the name of the variable to print.
  40. :w Width of the print field
  41. :d Decimal positions to print
  42. WriteLn(A:10:2); This statement would print the number, A, in a
  43. print field 10 characters wide, with 2 decimal positions.
  44. Look at the outputs on the screen again. The formats used are
  45. printed at the left. Square brackets are printed to show the
  46. width of the field and where the numbere is printed (left or
  47. right-justified).
  48. :-w Width of print field, left-justify the number.
  49. Notice the use of the minus sign to force printing of the number
  50. at the left of the field.
  51. What happens to the print field width when numbers are printed
  52. left-justified?
  53. ##### DO:
  54. Run PROG16 with 3.4567 as input.
  55. What happens when a print format is specified which will not hold
  56. all of the significant digits? Is the number rounded?
  57. Œ
  58. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 76
  59. 3. Calculations with Real Numbers.
  60. ##### DO:
  61. In PROG16 change the ReadLn(A) to ReadLn(B).
  62. Add after ReadLn(B):
  63. A := B + C;
  64. The result printed will be the sum of B, which you enter, and C
  65. which is a constant, -2.0.
  66. Run the program with 2.34 as input.
  67. Are the results as expected?
  68. In scientific calculations, very large, and very small numbers
  69. are sometimes needed. Can you enter these in a convenient form
  70. without a long string of zeros?
  71. The radius of the earth is 6370000 meters.
  72. This is the same as 6.37 times 100000
  73. Which is the same as 6.37 times 10 to the 6th power
  74. Which may be entered as 6.37E6 or 6.37E06
  75. or 6.37E+6 or 6.37E+06
  76. ##### DO:
  77. Run the program with 6.37E6 as input.
  78. Try the other 3 forms listed above.
  79. Are the results the same in all cases?
  80. Œ
  81. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 77
  82. ##### DO:
  83. Run the program with 6370000 as input.
  84. Does it make any difference whether the number is input in
  85. scientific notation or in the usual form?
  86. ##### DO:
  87. Run the program with 6.37E7 as input.
  88. What happens when the result is too large for the format?
  89. (Count the character positions used to print A:10:2).
  90. ##### DO:
  91. Get a new copy of the original PROG16.
  92. Run the program with 6.37E-6 as input.
  93. Notice the unformatted output is correct, but the formatted
  94. output shows nothing but zeros.
  95. The result, A, is 0.00000637, with significant digits too far to
  96. the right to show up in the formatted output.
  97. ##### DO:
  98. Change the WriteLn format, A:10:2 to A:10:6. (Notice there are
  99. two A:10:2's in the statement).
  100. Run the program with the following values:
  101. 6.37E6, 6.37E-6, 6.37E-4
  102. Œ
  103. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 78
  104. 4. Calculations with Integers and Real Numbers.
  105. What happens when you mix Integers and Real numbers in
  106. calculations?
  107. ##### DO:
  108. Get a copy of the original PROG16 and make the following changes:
  109. Change ReadLn(A); to ReadLn(I);
  110. Change the prompt to Write('Enter an Integer: ');
  111. Add after the ReadLn statement:
  112. J := I + C;
  113. Run the program.
  114. What results did you get?
  115. The "Type mismatch" refers to J.
  116. Since the calculation involves both an integer, I, and a real
  117. number, C, the result cannot be stored in an integer variable.
  118. If the result had significant digits after the decimal, they
  119. would be lost when stored as an integer.
  120. ##### DO:
  121. Change the calculation to:
  122. A := I + C;
  123. Run the program.
  124. NOTE: ON YOUR OWN, YOU MAY WANT TO EXPERIMENT WITH THE WRITELN
  125. FORMATS. CHANGE THE FORMATS TO VARIOUS VALUES AND TRY THEM
  126. WITH A VARIETY OF INPUTS.
  127. Œ
  128.