checking for overflow in C

Chris Dollin kers at otter.hpl.hp.com
Tue May 9 18:15:02 AEST 1989


Doug Gwyn said:

| 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.

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".



More information about the Comp.lang.c mailing list