home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / programm / 2433 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  1.3 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!ames!agate!lhasa.berkeley.edu!lippin
  2. From: lippin@lhasa.berkeley.edu (The Apathist)
  3. Newsgroups: comp.programming
  4. Subject: Re: Detecting Integer Overflow/Underflow
  5. Date: 23 Aug 1992 03:50:30 GMT
  6. Organization: Authorized Service, Incorporated
  7. Lines: 21
  8. Message-ID: <1771u6INN4eq@agate.berkeley.edu>
  9. References: <1992Aug21.182206.15853@progress.com> <HUDGENS.92Aug22113851@sun13.SCRI.FSU.EDU> <1572@idacrd.UUCP>
  10. Reply-To: lippin@math.berkeley.edu
  11. NNTP-Posting-Host: lhasa.berkeley.edu
  12.  
  13. Jim Hudgens <hudgens@SCRI.FSU.EDU> asks for a portable way to detect
  14. integer over- and underflow.
  15.  
  16. Some replies have appeared that display clear knowledge of the two's
  17. complement representation, and great prowess with bitwise operations.
  18.  
  19. However, for the sake of sanity -- both yours and that of those who
  20. program around you -- I suggest using this algorithm:
  21.  
  22.     a+b will overflow when a > 0 and b > maxint-a,
  23.     and will underflow when a < 0 and b < minint-a.
  24.  
  25. It would be a strange implementation indeed which did not satisfy
  26. these rules.
  27.  
  28.                     --Tom Lippincott
  29.                       lippin@math.berkeley.edu
  30.  
  31.     "This view derives partly from what is known as common sense,
  32.      whose virtue, uniquely among virtues, is that everyone has it."
  33.                     --Tom Stoppard, _Jumpers_
  34.