TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Rob Basler
from: Stephen Lindholm
date: 1994-11-13 21:43:48
subject: Need sqrt() algorithm

Rob Basler le escribe a All:

RB> I need an algorithm for calculating square roots to 3 or 4
RB> decimal places.  It must be fast, I don't care about

From C_ECHO, author cut off so that it would compile, but it was PD, anyway :):

/* ISqrt (I) computes INT (SQRT (I)), that is, the integral part of the */
/* square root of integer I. It does not check for negative arguments.  */
/* For all arguments 0..MaxInt the correct result is returned. The      */
/* algorithm exploits the following property:                           */
/*          n                                                           */
/*  n**2 =  Sigma (2i-1)                                                */
/*          i=1                                                         */

int ISqrt(int i)
{
  asm {
    mov   cx,i   ; load argument
    mov   ax,-1  ; init result
    cwd          ; init odd numbers to -1
    xor   bx,bx  ; init perfect squares to 0
{at}loop:
    inc   ax     ; increment result
    add   dx,2   ; compute next odd number
    add   bx,dx  ; next perfect square
    cmp   bx,cx  ; perfect square > argument ?
    jbe   {at}loop  ; until square greater than argument
  }
}

loSmaH cha'

--- timEd-B9

* Origin: Until the People Come Home (1:283/120.1)
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: 283/120 121 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™.