home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pascal / 6838 < prev    next >
Encoding:
Text File  |  1992-11-23  |  1.8 KB  |  48 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!news.ccs.queensu.ca!mast.queensu.ca!dmurdoch
  3. From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
  4. Subject: Re: I found a compiler bug in TP6.0, maybe...
  5. Message-ID: <dmurdoch.300.722537145@mast.queensu.ca>
  6. Lines: 36
  7. Sender: news@knot.ccs.queensu.ca (Netnews control)
  8. Organization: Queen's University
  9. References: <1992Nov23.061222.3675@u.washington.edu> <JgLouB1w165w@abyss.wa.com> <1eqvj5INNprl@bigboote.WPI.EDU>
  10. Date: Mon, 23 Nov 1992 16:45:46 GMT
  11.  
  12. In article <1eqvj5INNprl@bigboote.WPI.EDU> gregs@wpi.WPI.EDU (Floppy) writes:
  13.  
  14. >>> >  writeln(days[num-1]);
  15.  
  16. >    This really is a bug.  The manual clearly states that when an
  17. >arithmetic expression contains two integral types, both numbers will first be
  18. >converted to the smallest integral type containing the entire range of both of 
  19. >the original types.  In this case, the smallest such type is longint, since
  20. >the two original types were word and shortint.
  21.  
  22. No, that's not how the expression is parsed.  It's not "(num) + (-1)", it's
  23. "(num) - (1)".  Both "num" and "1" are compatible with the word type, so 
  24. that's how the expression is evaluated.
  25.  
  26. It's dangerous to rely on the automatic conversions, because it's not always 
  27. obvious how the compiler will apply them.  One that may surprise, but which 
  28. is compatible with the language definition, is the following:
  29.  
  30.   var
  31.     b1,b2 : byte;
  32.   begin
  33.     b1 := 255;
  34.     b2 := 255;
  35.     if b1*b2 < 0 then
  36.       writeln('Surprise!');
  37.   end.
  38.  
  39. BTW, I've just discovered that this is a bug in BP 7.0:  it behaves the same 
  40. as TP 6 in this expression, even when overflow checking is enabled.  It does 
  41. an unsigned multiply (which doesn't overflow), checks for overflow (and 
  42. doesn't see any), then does a signed comparison with 0 and sees a negative.
  43.  
  44. Duncan Murdoch
  45.  
  46. Duncan Murdoch
  47. dmurdoch@mast.queensu.ca
  48.