> From: Wouter van Ooijen[SMTP:wouter@VOTI.NL] > Sent: Thursday, March 11, 2004 2:48 AM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [PIC]: coding challenge: divide by 7 >> 1 / 7 = 9 / 63 >> To divide by 63, multiply by binary 0.000001000001000001 . . . >> To multiply by 9, multiply by binary 1001 >> >> To combine the two, multiply by >> 0.001001001001001001 . . . >> >> Repeatedly shift 3 places right and add. >> >> John Power > A kind of power series :) > I tried this. For a byte input the third and following terms will be 0, > so the formula is > x = ( y >> 3 ) + ( y >> 6 ) > This is a reasonable approximation, but not exact. > Wouter van Ooijen 1/7 is exactly equal to 0.001001001 ... if the series is carried to infinity. To get a reasonable result with finite number of terms, it is necessary to carry the fractional part for at least some reasonable number of iterations. The lack of a fractional part causes the error you are referring to. The first shift of 3 bits will create a fraction which is lost in integer arithmetic. The second shift does likewise. If those fractional parts were retained, their sum would sometimes increment the quotient, giving the right answer in those cases where the formula fails as given. John Power -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads