Detecting overflow

Larry Jones scjones at sdrc.UUCP
Mon May 22 00:25:21 AEST 1989


In article <1076 at garcon.cso.uiuc.edu>, mcdaniel at uicsrd.csrd.uiuc.edu (Tim McDaniel) writes:
> Under pANS C, "(a*b)/b" cannot be optimized to "a".  However, if it's not
> pANS C-compliant, all bets are off.  (That's the reason for having a
> standard in the first place, so that at least SOME bets can be made.
> :-) [where a and b are both integers]

Sorry, but you're wrong -- pANS DOES allow the expression to be
optimized provided the optimized expression gives the same result
as the original expression in all cases where the result is well
defined.  If a and b are both integers, then the expression "a"
does give the same result as "(a*b)/b" except when "(a*b)/b"
overflows.  If it overflows, the results are undefined and
getting the "correct" answer without generating an overflow is
certainly an allowable response to undefined behavior.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You can't get a body like mine in a bottle --
unless you push REAL HARD." - Judy Tenuta / Dr. Pepper



More information about the Comp.lang.c mailing list