From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fedc2d05e82c9174 X-Google-Attributes: gid103376,public From: robert_dewar@my-dejanews.com Subject: Re: Calculating SQRT in ADA Date: 1999/03/25 Message-ID: <7dbv6t$4u5$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 458694619 References: <36F913E0.75F51763@lmco.com> <19990324125518.01416.00000432@ngol08.aol.com> X-Http-Proxy: 1.0 x17.dejanews.com:80 (Squid/1.1.22) for client 205.232.38.14 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Thu Mar 25 00:15:34 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.04 [en] (OS/2; I) Date: 1999-03-25T00:00:00+00:00 List-Id: In article <19990324125518.01416.00000432@ngol08.aol.com>, johnherro@aol.com (John Herro) wrote: > Here's a generic square root routine in Ada 83, taken from my AdaTutor program: > > generic > type Dummy is digits <>; > function Sqrt(X :in Dummy) return Dummy; > function Sqrt(X :in Dummy) return Dummy is > Guess : Dummy := X; > begin > if X < 0.0 then > raise Constraint_Error; > end if; > while X /= 0.0 and then > abs(Guess*Guess/X - 1.0) > 3.0*Dummy'Epsilon loop > Guess := (X/Guess + Guess) * 0.5; > end loop; > return Guess; > end Sqrt; Why the epsilon test, this iteration will terminate to exact equality in any reasonable floating-point model, and you cannot guarantee the 3*epsilon convergence if the floating-point model is silly anyway. -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own