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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.unit0.net!news.nask.pl!news.nask.org.pl!newsfeed.pionier.net.pl!pwr.wroc.pl!news.wcss.wroc.pl!not-for-mail From: antispam@math.uni.wroc.pl Newsgroups: comp.lang.ada Subject: Re: Fun with Unbounded Rational Numbers Date: Sun, 9 Apr 2017 00:30:47 +0000 (UTC) Organization: Politechnika Wroclawska Message-ID: References: NNTP-Posting-Host: hera.math.uni.wroc.pl X-Trace: z-news.wcss.wroc.pl 1491697847 17691 156.17.86.1 (9 Apr 2017 00:30:47 GMT) X-Complaints-To: abuse@news.pwr.wroc.pl NNTP-Posting-Date: Sun, 9 Apr 2017 00:30:47 +0000 (UTC) Cancel-Lock: sha1:7F37BorMzPyodlZcMtvjMMEd/+0= User-Agent: tin/2.2.1-20140504 ("Tober an Righ") (UNIX) (Linux/4.9.5 (x86_64)) Xref: news.eternal-september.org comp.lang.ada:46545 Date: 2017-04-09T00:30:47+00:00 List-Id: Jeffrey R. Carter wrote: > I decided to see what happens when you calculate square roots using Newton's > method and the unbounded rational numbers in PragmARC.Rational_Numbers. Some > interesting things happen. > > For example, trying to calculate sqrt(2) to a small enough accuracy (0.000001 > will do it) results in the calculation of the next estimate > > M := Two * X; > X := X - Y / M; > > taking a Long Time (it does conclude eventually). > > More alarming, though, is that for some values, even a fairly large accuracy > will quickly result in Storage_Error. For example, sqrt(1.28) to an accuracy of > 0.01 will do it. > > I realize that square roots are often irrational, but the accuracies seem so > large that I didn't anticipate consuming GB of memory. Just naive, I guess. There is somthing wrong with your code (or less likely with PragmARC.Rational_Numbers). Using computer algebra system FriCAS and the following naive code: newton(y : Fraction(Integer), eps : Fraction(Integer)) : Fraction(Integer) == x : Fraction(Integer) := 1 while abs(y - x^2) >= eps repeat x := (x + y/x)/2 x I can compute newton(2, 1/10^10000) with almost no time. More precisely time for actual computation is of order of timing resolution, that is 10ms, while time needed to print result (that is binary to decimal convertion) is of order 20ms. The result has 12544 characters. FriCAS after each rational operation cancels common factors between numerator and denominator, this limits growth of numbers and is crucial for bigger calculations. But even without canceling common factors your computation should work. Namely size of numbers after each operation is approximately sum of sizes of arguments. Since y above does not change size of y/x is bounded by size of x plus a constant. So in iteration n + 1 size of x is bounded by two times size of x in iteration n plus a constant. Each iteration of Newton iteration approximately doubles accuracy. The total effect is that size of final result is proportional to accuracy, and low accuracy (relatively large eps) result should be small. -- Waldek Hebisch