---- START NEW MESSAGE --- Received: from cherry.ease.lsoft.com [209.119.0.109] by dpmail10.doteasy.com with ESMTP (SMTPD32-8.05) id A7965820007A; Fri, 30 Jan 2004 14:15:50 -0800 Received: from PEAR.EASE.LSOFT.COM (209.119.0.19) by cherry.ease.lsoft.com (LSMTP for Digital Unix v1.1b) with SMTP id <6.00CC5775@cherry.ease.lsoft.com>; Fri, 30 Jan 2004 17:15:39 -0500 Received: from MITVMA.MIT.EDU by MITVMA.MIT.EDU (LISTSERV-TCP/IP release 1.8e) with spool id 6907 for PICLIST@MITVMA.MIT.EDU; Fri, 30 Jan 2004 17:15:29 -0500 Received: from MITVMA (NJE origin SMTP@MITVMA) by MITVMA.MIT.EDU (LMail V1.2d/1.8d) with BSMTP id 2176; Fri, 30 Jan 2004 17:14:18 -0500 Received: from mail.bcpl.net [204.255.212.10] by mitvma.mit.edu (IBM VM SMTP Level 430) via TCP with ESMTP ; Fri, 30 Jan 2004 17:14:17 EST X-Comment: mitvma.mit.edu: Mail was sent by mail.bcpl.net Received: from ppp6.bcpl.net (ppp6.bcpl.net [207.19.142.20]) by mail.bcpl.net (8.12.9/8.12.9) with SMTP id i0UKaS9Q011250 for ; Fri, 30 Jan 2004 15:36:28 -0500 (EST) Received: by ppp6.bcpl.net with Microsoft Mail id <01C3E746.C47D9F80@ppp6.bcpl.net>; Fri, 30 Jan 2004 15:36:02 -0500 Encoding: 81 TEXT Message-ID: <01C3E746.C47D9F80@ppp6.bcpl.net> Date: Fri, 30 Jan 2004 15:35:59 -0500 Reply-To: pic microcontroller discussion list Sender: pic microcontroller discussion list From: "John N. Power" Subject: Re: [PIC:] divide by 10 question To: PICLIST@MITVMA.MIT.EDU Precedence: list X-RCPT-TO: Status: U X-UIDL: 371856590 > From: Jim Korman[SMTP:jkorman@ALLTEL.NET] > Sent: Wednesday, January 28, 2004 8:27 PM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [PIC:] divide by 10 question > Michael Rigby-Jones wrote: >>-----Original Message----- >>From: Andrew Kieran [mailto:akieran@UREACH.COM] >>Sent: 28 January 2004 15:00 >>To: PICLIST@MITVMA.MIT.EDU >>Subject: Re: [PIC:] divide by 10 question >> >> >>There are many solutions, but two in particular that I would >>consider: >> >>1) >>o starting with number to be divided in 'value1' >>o Shift right, store result in 'value1' and 'value2' >>o shift right, 'value1' two more times, storing result in >>'value1' o subtract 'value2' from 'value1' o The result is the >>original 'value1' divided by 10 >> >>This works likes this: >>rrf - divides by two, store in 'value2' >>rrf - now divided by four >>rrf - now divided by eight >>subtract 'value2' - now divided by ten >> To divide an integer N by 10, do this: 1. Multiply N by 1.5 Call the result B. 2. Divide B by 15. To perform step 1, shift N 1 bit right. Call the result D. Then B = N + D To divide B by 15, repeatedly perform the following steps: set quotient = 0 loop: shift B 4 places right and call the result the new B add B to quotient determine whether adequate accuracy has been obtained (more about this below) if sufficient, quotient is the integer part of the result else go to loop This will give the exact quotient only in the limit that the number of iterations approaches infinity. Since only an integer result is desired, the process can be stopped when the integer part of quotient stops changing. This is a workable criterion for termination. Either test quotient for change, or empirically determine a lower limit on the number of iterations which will always give the correct integer result for a given number of bits in N. Pseudo code statement of the process: (see following paragraph for statement about number format.) B = N + (N >> 1); for ( i = 0, quotient = 0; i < MAX_ITERS; i++ ) { quotient = quotient + (B >> 4); } For the intermediate results to be meaningful, a fractional part must be carried during the shifts and additions. One or more bytes must be appended to the right end of B, and the composite "register" considered to be a fixed point number. The number of bits required for these "guard bits" depends on the bit length of N. This number, as well as the smallest number of iterations which will guarantee correct integer part can be determined "off line" through experimentation. The fractional part is discarded, of course, when the process is complete. This result is based on a general rule. This is that dividing a number by (2 ^ M - 1) can be replaced by repeatedly shifting M bits right and accumulating a partial sum. (Infinitely, in principle, but finitely in practice.) John Power -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body .