Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: checking for overflow in C
Message-ID: <1989May10.161549.29747@utzoo.uucp>
Organization: U of Toronto Zoology
References: <13367@dartvax.Dartmouth.EDU> <1670018@otter.hpl.hp.com>
Date: Wed, 10 May 89 16:15:49 GMT

In article <1670018@otter.hpl.hp.com> kers@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@zoo.toronto.edu
