[comp.sys.mac.programmer] Bug in MPW Pascal 2.0.2 ???

mystone@caen.engin.umich.edu (Dean Yu) (07/11/89)

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 can compile and link this without getting any rude messages,
>but when I run it, it prints
>   -5536
>when it should print
>   60000
>. Does anybody out there know whether this is
>   a) a previously known bug
>   b) a previously unknown bug
>   c) me demanding too much of the compiler?
>

  While I can't say if this is actually a bug or not, I would like to point
out that -5536 is the two's complement of 60000.  If the variable u is
being compiled as an integer, this would be correct.  So the question is,
should u have been compiled as a longint or an integer?  Human logic says that
the compiler should recognize that the range for u is larger than MaxInt, and
should therefore make u a long integer, but the compiler might be thinking that
it can store 65535 in one word and made u an integer...  

_______________________________________________________________________________
Dean Yu                            | E-mail: mystone@{sol,caen}.engin.umich.edu
University of Michigan             | Real-mail: Dean Yu
Computer Aided Engineering Network |            909 Church St
                                   |            Apt C
===================================|            Ann Arbor, MI 48104
                                   | Phone: Given on a need to know basis, and
"I am the Merit Host.  I speak for |        only if you're going to offer me a
 the bitstream."  (In other words, |        job...
 these are my very own opinions;   | 
 my employer wants to have nothing |===========================================
 to do with them, or me.)          |       This space available for rent
-------------------------------------------------------------------------------

d88-bli@nada.kth.se (Bo Lindbergh) (07/12/89)

Consider the following Pascal program.

   program bug;
      var
         u  : 0..65535;
   begin
      u:=60000;
      writeln(u);
   end.

I can compile and link this without getting any rude messages,
but when I run it, it prints
   -5536
when it should print
   60000
. Does anybody out there know whether this is
   a) a previously known bug
   b) a previously unknown bug
   c) me demanding too much of the compiler?

Thanks in advance,
   Bo Lindbergh

cjp@Apple.COM (Chris Plummer) (07/12/89)

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 can compile and link this without getting any rude messages,
>but when I run it, it prints
>   -5536
>when it should print
>   60000
>. Does anybody out there know whether this is
>   a) a previously known bug
>   b) a previously unknown bug
>   c) me demanding too much of the compiler?

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.

	    --Chris Plummer

d88-bli@nada.kth.se (Bo Lindbergh) (07/13/89)

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?

In confusion,
   Bo Lindbergh

keith@Apple.COM (Keith Rollin) (07/13/89)

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

Greg@AppleLink.Apple.Com (Greggy) (07/13/89)

In article <445d59a8.1285f@maize.engin.umich.edu> 
mystone@caen.engin.umich.edu (Dean Yu) writes:
> [ ... discussions by various people about subranges fitting into 
>   different memory sizes... VAR u : 0..65535 ]
> Human logic says that the compiler should recognize that the range
> for u is larger than MaxInt, and should therefore make u a long
> integer, but the compiler might be thinking that it can store 65535
> in one word and made u an integer...  

Actually, human logic (and the Pascal language) says that if I declared a 
range of 0..65535, then by gosh the only numbers that had better come in 
and out of those variables better not go beyond the range I set!  Period.  
I don't care how the compiler implements it.  (Well, that's not entirely 
true.  I DO have to live with the Mac ROM, and it kind of cares... a 
little... %-b )

  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  + Greg Marriott               +                    AppleLink: Greg +
  + Just Some Guy               +                                    +
  + "My phone is always busy"   + Internet: Greg@AppleLink.Apple.Com +
  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  + Apple Computer, Inc.                                             +
  + 20525 Mariani Ave, MS-46z, Cupertino, CA  95014                  +
  + (408)974-busy                                                    +
  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

d88-bli@nada.kth.se (Bo Lindbergh) (07/13/89)

In article <33096@apple.Apple.COM> keith@Apple.COM (Keith Rollin) writes:
[about 50 lines deleted]
>
>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).

That's it! What you get is a schizophrenic variable which is unsigned
when you assign to it and signed when you access it. Now, if I could
get the opposite (signed store, unsigned fetch) too, I would definitely
call it a feature and not a bug... :-)

Thanks, Keith. I consider myself helped.

In a state of enlightenment,
   Bo Lindbergh