17.07.2016 Views

COAET" • r

Apple-Orchard-v3n2-1982-May-Jun

Apple-Orchard-v3n2-1982-May-Jun

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Based on the above discussion, we would expect something<br />

like the following output:<br />

0.12345600<br />

1.23456000<br />

12.34560000<br />

123.45600000<br />

1234.56000000<br />

12345.60000000<br />

123456.00000000<br />

1234560.00000000<br />

12345600.00000000<br />

123456000.00000000<br />

Instead, when we run the program, we get:<br />

0.123456M<br />

1.23456<br />

12.3456<br />

123.456<br />

1234.56<br />

12345.6<br />

123456.<br />

1.23456E6<br />

1.23456E7<br />

1.23456E8<br />

What's the problem here? Not only does Pascal fail to<br />

print the eight digits we requested after the decimal point, it<br />

also switches over to scientific notation after a certain point.<br />

And the extraneous "M" that appears after the first number<br />

is certainly a strange - er, "feature.'' I don't know what else<br />

to say about it. It seems that the output routines will display a<br />

maximum of six significant digits in a real number, presumably<br />

to protect the user from misinterpretinr the results of<br />

his/her program. The programmers who decided to implement<br />

real number formatting in this way no doubt had<br />

good reasons (in their own minds) for their decision, but the<br />

problem is that such behavior contradicts the documentation<br />

and most Pascal texts.<br />

So here's one possible solution to the problem; Listing 3<br />

is a simple routine to convert a real number to a formatted<br />

string, using the same parameters for minimum field width<br />

and digits after the decimal point:<br />

LISTING 3<br />

procedure rtos(var s: string; r: real;<br />

len, ndigits:integer);<br />

(* Convert real number (r) to a string (s) *)<br />

(* len = minimum field width *)<br />

(* ndigs = no. of digits after decimal pt. *)<br />

var<br />

negnum: boolean;<br />

sl: string[!];<br />

e, expo, i: integer;<br />

plO: real;<br />

begin<br />

s := I I ,<br />

' sl ! = I I;<br />

negnum := (r < 0.0);<br />

r := abs(r) + 0.5/pwroften(ndigits);<br />

expo := O;<br />

e := 32;<br />

while e > 0 do begin<br />

plO := pwroften(e);<br />

while r >= plO do begin<br />

r := r/plO;<br />

expo := expo + e<br />

end;<br />

e := e div 2<br />

end;<br />

for i := expo downto 0 do begin<br />

sl[l] := chr(trunc(r) + 48);<br />

s := concat(s, sl);<br />

r := 10.0 * (r - trunc(r))<br />

end;<br />

if ndigits > 0 then begin<br />

s : = conca t ( s, ' <strong>•</strong> ' ) ;<br />

for i := 1 to ndigits do begin<br />

sl[l] := chr(trunc(r) + 48);<br />

s := concat(s, sl);<br />

r := 10.0 * (r - trunc(r))<br />

end<br />

end;<br />

if negnum then<br />

s := concat('-', s);<br />

while length(s) < len do<br />

s := concat(' ', s)<br />

end;<br />

The routine could, for example, replace the statement:<br />

WRITELN(X: W: D);<br />

with the two statements:<br />

RTOS(S, X. W, D);<br />

WRITELN(S)<br />

Disclaimer: Listing 3 isn't meant to be a general-purpose<br />

real-to-string routine, just an indication of a possible solution.<br />

It doesn't take too much care in checking its input<br />

parameters, and it returns "noise" digits in the output string<br />

if one attempts to obtain more digits than Pascal can<br />

represent accurately. But enhancements to correct these<br />

features are easy to add; they are left to the interested<br />

reader.<br />

Conclusion<br />

I've tried to demonstrate three problems in Apple's<br />

implementation of Pascal. I don't want to make more out of<br />

it than is necessary; seen in the context of the whole Pascal<br />

system, they are relatively minor surprises that the majority<br />

of programmers will perhaps never come across. Apple<br />

Pascal, in my opinion, is still an excellent choice for software<br />

development.<br />

On the other hand, I don't want to minimize the seriousness<br />

of such language quirks. They are undocumented<br />

pitfalls that the programmer must discover and avoid, so<br />

they do make one's programming effort more difficult.<br />

They also damage one of Pascal's nicest benefits: its portability.<br />

A Pascal program written on the Apple can be moved<br />

(in theory) to another computer and run with relatively little<br />

modification, and vice versa. But deviations and -er, "features"<br />

like these can make such modifications much more<br />

difficult.<br />

58 Apple Orchard

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!