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