You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
here is: Don't always expect books to tell you what the<br />
computer is going to do.<br />
2) Real Number Input<br />
It's a frustrating thing to be forced to write your own<br />
routine to accomplish a certain task when you know.that<br />
such a routine already exists, but is inaccessible to you. One<br />
such example in the Pascal system is the conversion of a<br />
string of characters into a number. The system "knows"<br />
how to do the translation - it happens every time a value is<br />
typed in at the keyboard or read from a text file. But if you<br />
want your program to do the same thing, you'll have to write<br />
your own procedure to do it, because the system hides the<br />
procedure from you.<br />
Let's assume you want to write a routine to translate a<br />
string of characters into a real number. One of the things<br />
you should know while writing such a module is the smallest<br />
and largest positive real numbers the system can represent,<br />
so your routine can avoid over- and underflows. To determine<br />
these values, you might write a program like this:<br />
program readreal;<br />
var<br />
x: real<br />
begin<br />
while TROE do<br />
begin<br />
write('Enter x':);<br />
readln(x);<br />
writeln('x = ',x)<br />
end<br />
end.<br />
This program will blow up with a floating point execution<br />
error if you type in a number too large or too close to zero.<br />
With experimentation, you'll find that the largest real<br />
number Pascal can represent is approximately 3.40E38,<br />
and the smallest positive real number is approximately<br />
l.18E-38. Or, more accurately, these are the limits on the<br />
numbers one can enter using the READ procedure.<br />
A problem arises when you try to incorporate the smaller<br />
number into a program. If you use the lines:<br />
const<br />
TINY = 1.18E-38<br />
or even:<br />
IF X >+ l.18e-38 THEN ...<br />
. .. the Pascal compiler will ·(Kaboom!) self-destruct on<br />
attempting to translate the number. To be more precise, the<br />
compiler program itself will halt with a floating point execution<br />
error.<br />
What does this mean? Apparently, there are actually two<br />
different routines in the Pascal system for translating strings<br />
of characters into real numbers: one used by the READ<br />
statement, and one used by the compiler. And the routine<br />
the compiler uses will not translate some numbers which<br />
offer no problem to the other one.<br />
Here's an example of another symptom of having two<br />
different real number conversion routines:<br />
program realtest;<br />
var<br />
x: real;<br />
56 Apple Orchard<br />
begin<br />
repeat<br />
write('Enter x':);<br />
readln(x);<br />
writeln('x = ',x)<br />
until x = 2. 718281828<br />
end.<br />
Most programmers know better than to compare two<br />
floating point numbers for exact equality. But in this example<br />
we might reasonably expect such a comparison to work<br />
If you enter "2.718281828" the program should react normally,<br />
shouldn't it? Well, in Apple Pascal, it doesn't. The two<br />
different conversion routines translate the strings (in the<br />
program and in the READ statement) into two ever so<br />
slightly different floating point representations. The comparison<br />
fails, and the program continues on.<br />
There's no good reason why there should be more than<br />
one such routine in the Apple Pascal system. (The one you<br />
may have to write will be at least the third!) A major benefit of<br />
using a modular programming language like Pascal is that<br />
one can write a single reliable procedure to accomplish a<br />
given task, and use it in a variety of applications. The moral:<br />
Duplicated programming effort is not only wasteful, but it<br />
can lead to -er, "features"-<br />
in your programs.<br />
3)Real Number Output .<br />
We just discussed the frustration one feels when a routine<br />
must be written to do something the system already knows<br />
how to do. Another kind of pain results when the language<br />
texts and system documentation spell out in detail what. a<br />
given funtion does in different circumstances -.and th~n m<br />
practice the function actually does something entirely<br />
different.<br />
Such is the case with the standard procedures WRITE or<br />
WRITELN when they are used to output real numbers. Most<br />
texts, as well as Apple's own documentation, (see the Apple<br />
Pascal Language Reference Manual, Pages 36-37) describe<br />
the procedure as follows: The statement:<br />
WRITELN(X: W: D)<br />
... will cause the real number X to be written, after it is<br />
converted to a string. The optional variables W and D control<br />
the output format; Wis the minimum number of characters<br />
that will be printed, and D is the number of digits to be<br />
printed after the decimal point. .<br />
So far, so good. Consider the program m Listing 2:<br />
LISTING 2<br />
program testwrite;<br />
var<br />
w, d, i: integer;<br />
x: real;<br />
begin<br />
x := 0.123456;<br />
w:=20;<br />
d := 8;<br />
for i := 1 to 10 do<br />
begin<br />
writeln(x: w: d);<br />
x*·(lO*x)<br />
end<br />
end.<br />
'<br />
,