TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 74 TURBO-LESSON 16: REAL NUMBERS OBJECTIVES - In this lesson, you will learn about: 1. Range of Real Numbers 2. Input/Output of Real Numbers 3. Calculations with Real Numbers 4. Calculations with Integers and Real Numbers 1. Range of Real Numbers For business processing (dollar amounts), and scientific processing, integers alone are not adequate. Decimal numbers and sometimes numbers in scientific notation are needed. TURBO provides Real Numbers with 11 significant digits of precision in the range: 1E-38 to 1E+38 (The BCD version of TURBO provides 18 significant digits and a range of 1E-63 to 1E+63, but this set of TURBO-LESSONS deals with the more limited range above.) ##### DO: Run PROG16. Enter 444.333222111 and examine the result presented in scientific notation. How many digits are retained before the E? (I counted 11, including digits on both sides of the decimal point.) This is the 11 significant digits of precision. Note that the last 1 you entered was dropped. Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 75 2. Input/Output of Real Numbers. You will need to know what to expect with various combinations of input and output of real numbers. ##### DO: Run PROG16. Enter 444.333222111 and study the various outputs. When the real number, A, is output without any formatting, scientific notation is used: 4.4433322211 are the significant digits. E+02 means multiply by 10 to the 2nd power to get the number. If you want the number presented in some other form, you can add the :w:d formatting to the name of the variable to print. :w Width of the print field :d Decimal positions to print WriteLn(A:10:2); This statement would print the number, A, in a print field 10 characters wide, with 2 decimal positions. Look at the outputs on the screen again. The formats used are printed at the left. Square brackets are printed to show the width of the field and where the numbere is printed (left or right-justified). :-w Width of print field, left-justify the number. Notice the use of the minus sign to force printing of the number at the left of the field. What happens to the print field width when numbers are printed left-justified? ##### DO: Run PROG16 with 3.4567 as input. What happens when a print format is specified which will not hold all of the significant digits? Is the number rounded? Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 76 3. Calculations with Real Numbers. ##### DO: In PROG16 change the ReadLn(A) to ReadLn(B). Add after ReadLn(B): A := B + C; The result printed will be the sum of B, which you enter, and C which is a constant, -2.0. Run the program with 2.34 as input. Are the results as expected? In scientific calculations, very large, and very small numbers are sometimes needed. Can you enter these in a convenient form without a long string of zeros? The radius of the earth is 6370000 meters. This is the same as 6.37 times 100000 Which is the same as 6.37 times 10 to the 6th power Which may be entered as 6.37E6 or 6.37E06 or 6.37E+6 or 6.37E+06 ##### DO: Run the program with 6.37E6 as input. Try the other 3 forms listed above. Are the results the same in all cases? Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 77 ##### DO: Run the program with 6370000 as input. Does it make any difference whether the number is input in scientific notation or in the usual form? ##### DO: Run the program with 6.37E7 as input. What happens when the result is too large for the format? (Count the character positions used to print A:10:2). ##### DO: Get a new copy of the original PROG16. Run the program with 6.37E-6 as input. Notice the unformatted output is correct, but the formatted output shows nothing but zeros. The result, A, is 0.00000637, with significant digits too far to the right to show up in the formatted output. ##### DO: Change the WriteLn format, A:10:2 to A:10:6. (Notice there are two A:10:2's in the statement). Run the program with the following values: 6.37E6, 6.37E-6, 6.37E-4 Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 78 4. Calculations with Integers and Real Numbers. What happens when you mix Integers and Real numbers in calculations? ##### DO: Get a copy of the original PROG16 and make the following changes: Change ReadLn(A); to ReadLn(I); Change the prompt to Write('Enter an Integer: '); Add after the ReadLn statement: J := I + C; Run the program. What results did you get? The "Type mismatch" refers to J. Since the calculation involves both an integer, I, and a real number, C, the result cannot be stored in an integer variable. If the result had significant digits after the decimal, they would be lost when stored as an integer. ##### DO: Change the calculation to: A := I + C; Run the program. NOTE: ON YOUR OWN, YOU MAY WANT TO EXPERIMENT WITH THE WRITELN FORMATS. CHANGE THE FORMATS TO VARIOUS VALUES AND TRY THEM WITH A VARIETY OF INPUTS. Œ