| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Need sqrt() algorithm |
On Tuesday, 1994-11-08 Rob Basler wrote to All about "Need sqrt()
algorithm" as follows:
RB> I need to do a distance calculation between two points
RB> (x0,z0) and (x1,z1). The way I know to do this is to use
RB> the distance formula, but it requires a square root
RB> calculation.
Hi Rob,
I presume you want this in fairly standard C. The following function
returns the square root of an integer, but the root is multiplied by
100 to provide 2 decimal places. For example, the square root of 10 is
3.16 (to 2 places) and this routine returns 316. You place the decimal
point yourself when your calculations are complete. The places are
gained by multiplying the argument by 10000; to get more places
increase the number of trailing zeroes by 2 for each additional place
you need (e.g. for 3 places multiply by 1000000).
Your distance would then be:
dist = (isqrt((x1-x0)**2+(z1-z0)**2) + 50)/100;
The addition of 50 provides rounding instead of truncation. In 3
dimensions it would be:
dist = (isqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2) +50)/100;
Note that stdlib.h is needed for abs(). You can replace this if you
want.
I hope I understood your problem correctly. If not, provide me with
more details and I will try again.
Regards
Dave
___--------------------------------------------------------------
#include
/* The function isqrt() returns the square root
of an integer, multiplied by 100. This provides
2 decimal places; the decimal point is assumed
to be between the hundreds digit and the tens
digit.
It uses the Newton-Raphson algorithm, but in an
integer domain instead of real numbers. */
unsigned long isqrt(unsigned long N)
{
unsigned long t0,t1,t2;
t0 = N*10000; /* dilation for 2 decimal places */
t1 = t0/2;
if (t1 > 0)
do
{
t2 = t1;
t1 = (t0/t1 + t1)/2;
} while(abs(t2-t1) > 1);
return t1;
}
___-------------------------------------------------------------
* KWQ/2 1.2g * Multitasking: Make twice the mistakes in half the time.
--- Maximus/2 2.02
* Origin: OS/2 Shareware BBS, Fairfax, VA: 703-385-4325 (1:109/347)SEEN-BY: 12/2442 54/54 620/243 624/50 632/348 640/820 690/660 711/409 410 413 SEEN-BY: 711/430 807 808 809 934 942 949 712/353 623 713/888 800/1 @PATH: 109/347 2 7 3615/50 229/2 12/2442 711/409 54/54 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.