🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #911

Re: do...while vs. repeat...until (was: Errors aren't that simple)

#911
From: Georg Wittig
Date: Mon, 19 Mar 1990 14:10
29 lines
1679 bytes
pmontgom@oak.math.ucla.edu (Peter Montgomery) writes:

>	Another problem with do..while is that we cannot access
>variables declared inside the block in the loop test.  For example,
>when updating a square root by Newton's method, the code

>		double sqrtx, x;
>		sqrtx = (some approximation to sqrt(x))
>		do {
>		    register const double err = x - sqrtx*sqrtx;
>		    sqrtx += err/(2*sqrtx);
>		} while (fabs(err) > 0.001*x);

>is illegal.  Rather, we must declare "err" in a larger scope.

No, you needn't. The problem can be avoided easily:

		double sqrtx, x;
		sqrtx = (some approximation to sqrt(x))
		do {
		    register const double err = x - sqrtx*sqrtx;
		    sqrtx += err/(2*sqrtx);
		    if(! (fabs(err) > 0.001*x)) break;		/* HA! */
		} while (1);
-- 
Georg Wittig   GMD-Z1.BI   P.O. Box 1240   D-5205 St. Augustin 1 (West Germany)
email: wittig@gmdzi.uucp   phone: (+49 2241) 14-2294
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Freedom's just another word for nothing left to lose" (Kris Kristofferson)
Technical Details
Message-ID: <2104@gmdzi.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!mailrus!uunet!mcsun!unido!gmdzi!wittig
References: <8192@hubcap.clemson.edu> <17000003@hpcvra.CV.HP.COM> <Mar.7.10.43.42.1990.17402@paul.rutgers.edu> <5819.25f7a840@vax1.tcd.ie> <16188@haddock.ima.isc.com> <2401@sunset.MATH.UCLA.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!mailrus!uunet!mcsun!unido!gmdzi!wittig
From: wittig@gmdzi.UUCP (Georg Wittig)
Newsgroups: comp.lang.misc
Subject: Re: do...while vs. repeat...until (was: Errors aren't that simple)
Message-ID: <2104@gmdzi.UUCP>
Date: 19 Mar 90 14:10:35 GMT
References: <8192@hubcap.clemson.edu> <17000003@hpcvra.CV.HP.COM> <Mar.7.10.43.42.1990.17402@paul.rutgers.edu> <5819.25f7a840@vax1.tcd.ie> <16188@haddock.ima.isc.com> <2401@sunset.MATH.UCLA.EDU>
Organization: GMD - The German National Research Centre for Computer Science
Lines: 29