PICmicro & dsPIC >> PICmicro & dsPIC

Pages: 1
John Payson

****

Reged: Oct 16 2003
Posts: 1098
Distance function sqrt(a*a+b*b [+c*c...])
      #5509 - Tue Sep 03 2002 06:33 PM

Given two or three numbers, byte or int16, what is the best way to compute the "distance" function (square root of the sum of squares)?  Certainly computing the square root of the sum of squares is possible, but it seems needlessly expensive in terms of CPU time.  Can anyone offer a better algorithm for the PIC16x?


Post Extras: Print Post   Remind Me!   Notify Moderator  
Bill2

***

Reged: Oct 16 2003
Posts: 26
Re:Distance function sqrt(a*a+b*b [+c*c...])
      #5510 - Thu Sep 05 2002 01:22 PM

>Given two or three numbers, byte or int16, what
>is the best way to compute the "distance"
>function (square root of the sum of squares)?
>Certainly computing the square root of the sum of 
>squares is possible, but it seems needlessly 
>expensive in terms of CPU time.  Can anyone offer 
>a better algorithm for the PIC16x?

I can't offer a better algorithm, but I can
point you to a square root routine I used
when I need to take a square root of a 16-bit
integer as fast as possible.

It's in assembly, but can be "inlined" into C
code (what I did).

http://www.dattalo.com/technical/software/pic/picsqrt.html

Bill


Post Extras: Print Post   Remind Me!   Notify Moderator  
JanErik Bertram

*

Reged: Oct 16 2003
Posts: 83
Loc: Germany, Bochum
Re:Distance function sqrt(a*a+b*b [+c*c...])
      #5511 - Sun Sep 15 2002 11:55 AM

Hi John,

i have no solution for computing it faster, but
perhaps you don't have to compute it.
In one of my projects i need to find the right distance to an object.
I only compute a^2+b^2 for every object and check it against d^2 (i compute d^2 only once).


>Given two or three numbers, byte or int16, what is the best way to compute the "distance" function (square root of the sum of squares)?  Certainly computing the square root of the sum of squares is possible, but it seems needlessly expensive in terms of CPU time.  Can anyone offer a better algorithm for the PIC16x?


Post Extras: Print Post   Remind Me!   Notify Moderator  
John Payson

****

Reged: Oct 16 2003
Posts: 1098
Re:Distance function sqrt(a*a+b*b [+c*c...])
      #5512 - Mon Sep 16 2002 12:12 PM


>I only compute a^2+b^2 for every object and check it against d^2 (i compute d^2 only once).

In this application, for compatibility reasons, it is probably necessary to compute the actual distance (the PIC feeds numbers to another micro).  Since that other micro does a table lookup to compute the log of the numbers fed it, it may be possible to rework things to return the square rather than sqrt, but I recall reading somewhere that there was a way of computing or approximating the distance function which was as cheap or cheaper than computing the squares, let alone the square root.


Post Extras: Print Post   Remind Me!   Notify Moderator  
JanErik Bertram

*

Reged: Oct 16 2003
Posts: 83
Loc: Germany, Bochum
Re:Distance function sqrt(a*a+b*b [+c*c...])
      #5513 - Tue Sep 17 2002 05:55 AM

Hi, 

perhaps this helps

http://www.goldenchip.com.cn/download/material/appcorpus/02/05.pdf

ciao
Jan Erik Bertram


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
7 registered and 38 anonymous users are browsing this forum.

Moderator:  mikerj, jtemples, jeff, Dan Henry, Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 2206

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5