checking for overflow in C

Henry Spencer henry at utzoo.uucp
Thu May 11 02:15:49 AEST 1989


In article <1670018 at otter.hpl.hp.com> kers at otter.hpl.hp.com (Chris Dollin) writes:
>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?

Apart from the obvious (but costly, in C) possibility of doing multiprecision
arithmetic, you might look for a paper titled something like "Legality
Assertions in Euclid", by Dave Wortman, in a very early issue of IEEE
Transactions on Software Engineering (circa ten years ago).  It looks
at exactly this problem:  how to check for overflow, precisely (i.e. no
false alarms allowed), while being certain never to cause overflow in the
overflow checks themselves.  It's trickier than it looks.

(Apologies for the imprecise reference, my copy isn't handy.)
-- 
Mars in 1980s:  USSR, 2 tries, |     Henry Spencer at U of Toronto Zoology
2 failures; USA, 0 tries.      | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list