Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!otter!kers From: kers@otter.hpl.hp.com (Chris Dollin) Newsgroups: comp.lang.c Subject: Re: checking for overflow in C Message-ID: <1670018@otter.hpl.hp.com> Date: 9 May 89 08:15:02 GMT References: <13367@dartvax.Dartmouth.EDU> Organization: Hewlett-Packard Laboratories, Bristol, UK. Lines: 34 Doug Gwyn said: | In article <13367@dartvax.Dartmouth.EDU> shallit@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. Doug, Doug! How about, for an example off the top of my head (and sitting in a directory on my workstation) writing a bytecode interpreter? Presumably, you'll let me have a multiply instruction, and not constrain the byte-code generator to only use it when it can *prove* there's no overflow? Actually, I have exactly this problem. What *is* the approved way of doing long * long and generating either the long result, or an indication of overflow? And similarly for division? Yes, I'll look at Knuth; but are there some accepted idioms around? If it matters, for other reasons I'm probably restricted to 32-bit machines with 8-bit bytes where pointers-to-long and longs have the same size. Kers. | "A foolish hobgoblin has the consistency of little minds".