Path: utzoo!attcan!uunet!husc6!bloom-beacon!mit-eddie!bbn!uwmcsd1!csd4.milw.wisc.edu!markh From: markh@csd4.milw.wisc.edu (Mark William Hopkins) Newsgroups: comp.lang.pascal Subject: Re: Number-string conversions Summary: Verification Message-ID: <5858@uwmcsd1.UUCP> Date: 20 May 88 20:00:24 GMT References: <5817@uwmcsd1.UUCP> <5484@aw.sei.cmu.edu> Sender: daemon@uwmcsd1.UUCP Reply-To: markh@csd4.milw.wisc.edu (Mark William Hopkins) Organization: University of Wisconsin-Milwaukee Lines: 167 This is a followup from the previous article and addresses more directly the response of R. Firth several articles back concerning the nature of several number-to-string procedures listed previously: : > While the issue is "on the table", I feel it appropriate to give a few >procedures to show how the conversions take place when Pascal reads & writes >in a text file. ... ... : >I find it hard to express my feelings in a manner that is both clear and >polite. The above code is evidently rubbish; unfortunately it is typical >of the stuff produced by amateur software writers - you can find examples >galore, as bad or worse, in many packages distributed by major institutes >of education. > >These examples would not have survived any one of: accurate specification, >competent design review, code walkthrough, formal analysis, or thorough >testing. I conclude that they were subjected to none of these processes. > >Folks, are we surprised there is a "software crisis"? And what can we, >as responsible individuals, do about it? : (1) The procedure listings are for show only, they are not meant to be run. This much is obvious, since Pascal ALREADY has the facilities built-in. (2) The procedures are meant to SIMULATE Pascal ... for better or for worse ... flaws and all ... in order to demonstrate just how Pascal carries out the special built-in read procedures and to make explicit the limitations and flaws of these built-in procedures. But in any case, I feel that the criticism should be more directly addressed: So let us scrutinize the ReadInt procedure to see if it actually does simulate Pascal's integer read: THE LISTINGS: P A S C A L : program Pascal(input, output); var I : integer; begin read(I); write(I:1) end. R e a d I n t : program Simulate(input, output); var I : integer; ... (*** Procedure ReadInt inserted here ***) begin ReadInt(I); write(I:1) end. where the procedure listing is: procedure ReadInt(var T : text; var I : integer); var Sign : char; begin (*** SKIP SPACES ***) while T^ = ' ' do get(T); {! may run off the end of the file} (*** GET THE SIGN ***) if T^ in ['+','-'] then begin read(T, Sign) end else Sign := '+'; (*** CONVERT THE STRING TO AN INTEGER ***) I := 0; while T^ in ['0'..'9'] do begin I := 10*I + ord(T^) - ord('0'); {! may cause false overflow on '+' if number large} get(T) end; (*** ADJUST FOR THE SIGN ***) if Sign = '-' then I := -I {! and of course fails on the most negative integer} end; Let us look into some of the supposed flaws of the ReadInt procedure: (1) O V E R F L O W S In all fairness, I should mention that the way a compiler handles overflow is implementation dependent. The ReadInt procedure was promised to simulate how Pascal reads integers ... for better or for worse. Script started on Fri May 20 14:13:34 1988 T H E I N P U T F I L E : (csd4) 1: more In 123456789012345 H O W S T A N D A R D P A S C A L M A Y H A N D L E O V E R F L O W : (csd4) 2: pix Pascal.p : I := 10*I + ord(T^) - ord('0'); {! may cause false overflow on '+' if number large} get(T) end; ... So much for overflow ... (2) O V E R R E A D I N G T H E E N D - O F - F I L E : The question asked here is whether ReadInt successfully simulates the way Pascal reads integers when there are no integers to be read ... Script started on Fri May 20 14:17:01 1988 T H E I N P U T F I L E : (csd4) 1: more In H O W S T A N D A R D P A S C A L H A N D L E S I T : (csd4) 2: pix Pascal.p : 9 ---> while T^ = ' ' do get(T); {! may run off the end of the file} ... "Pray tell, your majesty ... excuse me, I mean 'Your Majesty' ... a simulation so perfect that it even simulates the flaws!" Now a few words about the procedures themselves. Nobody in their right mind would actually set up and run text read procedures that simulated what was already built-in. If I were actually going to write procedures to do text I/O they would have had ERROR CORRECTION facilities built in them.