checking for overflow in C

michael.n.condict mnc at cbnewsm.ATT.COM
Tue May 9 13:42:05 AEST 1989


In article <10218 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
> In article <13367 at dartvax.Dartmouth.EDU> shallit at eleazar.dartmouth.edu (Jeffrey Shallit) writes:
> >... enlighten me about the officially approved way of checking overflow when
> >multiplying two integers.
> 
> Patient:  Doctor, it hurts when I do this.
> Doctor:  Don't do that!
> 
> You're looking for tactics when what is needed is better strategy.
> How did you get your algorithm into the state where an overflow is
> even possible?  Sounds to me like the algorithm needs to be better
> engineered.

I think you're off-base here, Doug.  The obvious (and morally justifiable)
answer to your question is that you've read two numbers from the user's input
and the user requests you to multiply them.  A typical example would be an
interpreter for a language that is using the local machine integers as its
integer type.  I too am interested in knowing how such an interpreter, written
in portable C, can efficiently determine whether overflow will occur when it
multiplies two arbitrary integers.  The methods that come to my mind require
much more time than the multiply itself.  Please enlighten.

Michael Condict
AT&T Bell Laboratories
Murray Hill, NJ
{allegra|att}!m10ux!mnc



More information about the Comp.lang.c mailing list