On Sat, 23 Aug 2003, CVT-SSS wrote: > I was checking some links posted to my request and found a very simple > algorithm that I have translated to Visual Fox procedure and works well. > Thre original code sets to 2 the sqr_dif and the accuracy is arround +/- 8%, > then I found a better accuracy changing the sqr_dif factor to 1/10 obtaining > +/- 0.01%. I believe that is enough to make the measurement. Now Im going to > start the translation to make tjhe PIC code. > > The following procedure works in 9 cicles giving a good aproximation > > local number, sqr_root, sqr_done, sqr_dif, sqr_nmbr > store 0 to sqr_root, sqr_done, sqr_dif, sqr_nmbr > store 921 to number /* max value from 1024 * 4.5 / 5.0 > store 1 to sqr_dif > store number to sqr_root, sqr_done > > do while sqr_done > ( sqr_dif / 10 ) > sqr_nmbr = number / sqr_root > sqr_done = sqr_root - sqr_nmbr > sqr_root = ( sqr_root + sqr_nmbr ) / 2 > ? numero > ? sqr_root > ? sqr_done > enddo This algorithm is extraordinarily slow on a PIC. I wouldn't waste my time with it especially since there have been other algorithms posted that are faster and more importantly, working already. The reason this algorithm is slow is because of the division. You could, if you wanted, change the divsion to a multiplication (and modify the algorithm slightly), but that would still be slow. In fact it'd even be slow on the 18f part which is equipped with a hardware multiplier! (I've tried it). Scott -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body