Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!apple!keith From: keith@Apple.COM (Keith Rollin) Newsgroups: comp.sys.mac.programmer Subject: Re: Bug in MPW Pascal 2.0.2 ??? Message-ID: <33096@apple.Apple.COM> Date: 12 Jul 89 22:17:27 GMT References: <1270@draken.nada.kth.se> <33064@apple.Apple.COM> <1275@draken.nada.kth.se> Organization: Apple Computer Inc, Cupertino, CA Lines: 60 In article <1275@draken.nada.kth.se> d88-bli@nada.kth.se (Bo Lindbergh) writes: >In article <33064@apple.Apple.COM> cjp@Apple.COM (Chris Plummer) writes: >>In article <1270@draken.nada.kth.se> d88-bli@nada.kth.se (Bo Lindbergh) writes: >>>Consider the following Pascal program. >>> >>> program bug; >>> var >>> u : 0..65535; >>> begin >>> u:=60000; >>> writeln(u); >>> end. >>> >> >>I'm not sure about MPW Pascal 2.0.2, but the 3.0 manual says that the >>parameter must be of type char, integer, real, string, packed array of >>char, or boolean. Since an integer is -32768..32767, I'd imaging that >>it's simply typecasting "u" to an integer which will cause numbers greater >>that 32767 to be negative. You could assign "u" to a real and then >>pass the real to writeln. > >You mean printing longints isn't allowed? Since the type of u is a subrange >of longint, the compiler should complain when I try to pass it >as a parameter to writeln. And this program: > > program bugTwo; > var > l : longint; > begin > l:=60000; > writeln(l); > end. > >prints 60000 and nothing else. Finally, if I assign the value of u to a real, >the result is -5536. I don't think the problem has anything to do with writeln. >More suggestions, anyone? > It probably has to do with the backflips that the Pascal compiler goes through to implement this specific range (0..65535). Although you'd think that it would be stored as a longint (because it overlaps into the longint range), it is actually implemented as a short/word/2 bytes/whatever. When this value is passed to writeln, it is passed as a word, and is hence treated as a integer value. Why is (0..65535) treated special? Probably because of convenience. Some Toolbox and OS routines require an unsigned integer passed to them. C can handle this, but Pascal doesn't know about unsigned types. If the toolbox wants a 2 byte number in the positive range, this hack is required. Actually, through the magic of MultiFinder, I just tried out an experiment. It seems that types (0..xxxxx) where xxxxx < 65536, then variables of that type are treated as integers; this feature is not limited to just (0..65535). ------------------------------------------------------------------------------ Keith Rollin --- Apple Computer, Inc. --- Developer Technical Support INTERNET: keith@apple.com UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions