Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!bloom-beacon!athena.mit.edu!jfc From: jfc@athena.mit.edu (John F Carr) Newsgroups: comp.lang.c Subject: Re: Detecting overflow Message-ID: <11610@bloom-beacon.MIT.EDU> Date: 23 May 89 05:36:08 GMT References: <455@skye.ed.ac.uk> <723@tukki.jyu.fi> <1076@garcon.cso.uiuc.edu> <731@sdrc.UUCP> Sender: root@bloom-beacon.MIT.EDU Reply-To: jfc@athena.mit.edu (John F Carr) Organization: Massachusetts Institute of Technology Lines: 25 In article <731@sdrc.UUCP> scjones@sdrc.UUCP (Larry Jones) writes: >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. If they are unsigned integers, this optimization is not possible, since overflow is ignored (i.e. given n bit words, (2 * (2^n - 1)) / 2 == 2^(n-1) - 1 is required with unsigned math [becuase the intermediate value, 2*(2^n - 1) must be truncated to 2^n - 2], while the true answer is (2 * (2^n - 1)) / 2 == 2^n - 1 ). --John Carr (jfc@athena.mit.edu) John Carr "When they turn the pages of history, jfc@Athena.mit.edu When these days have passed long ago, bloom-beacon! Will they read of us with sadness athena.mit.edu!jfc For the seeds that we let grow?" --Neil Peart