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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.31.142.202 with SMTP id q193mr12655956vkd.29.1491660870373; Sat, 08 Apr 2017 07:14:30 -0700 (PDT) X-Received: by 10.157.38.101 with SMTP id a92mr1368056otb.20.1491660870326; Sat, 08 Apr 2017 07:14:30 -0700 (PDT) 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.glorb.com!g57no319554qta.0!news-out.google.com!j72ni64itb.0!nntp.google.com!y18no6085596itc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 8 Apr 2017 07:14:29 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:944a:5177:1a55:f7e1; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:944a:5177:1a55:f7e1 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Fun with Unbounded Rational Numbers From: Robert Eachus Injection-Date: Sat, 08 Apr 2017 14:14:30 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:46544 Date: 2017-04-08T07:14:29-07:00 List-Id: On Saturday, April 8, 2017 at 6:37:48 AM UTC-4, Jeffrey R. Carter wrote: > I decided to see what happens when you calculate square roots using Newto= n's=20 > method and the unbounded rational numbers in PragmARC.Rational_Numbers. S= ome=20 > interesting things happen. >=20 > For example, trying to calculate sqrt(2) to a small enough accuracy (0.00= 0001=20 > will do it) results in the calculation of the next estimate >=20 > M :=3D Two * X; > X :=3D X - Y / M; >=20 > taking a Long Time (it does conclude eventually). >=20 > More alarming, though, is that for some values, even a fairly large accur= acy=20 > will quickly result in Storage_Error. For example, sqrt(1.28) to an accur= acy of=20 > 0.01 will do it. >=20 > I realize that square roots are often irrational, but the accuracies seem= so=20 > large that I didn't anticipate consuming GB of memory. Just naive, I gues= s. Sigh! You are running through a minefield. Slowing to a walk will keep you= alive a few minutes longer, but to survive you have to take each step very= carefully. First, you are trying to calculate an approximation to an irrational number= . The algorithm promises AT LEAST quadratic convergence which means that t= here will be at least twice as many bits/digits/words to store at each step= . But that is only half the story. Your approximation will be a rational num= ber. (Add, subtract, multiply, divide on rationals result in rationals.) Th= e difference between your estimate and the actual irrational number is also= an irrational number. So when you test to a tolerance that subtraction sh= ould always raise Storage_Error. But you did not say what you are using as= an approximation of sqrt(2) for testing. I suspect you are squaring your = approximate value and testing that. Sounds good, but... For some reason you are using decimal limits. These can not be represented= accurately in binary, but using a rational package they will be represente= d by two numbers, with the divisor being a power of ten. How do you compar= e two numbers stored as the ratio of integers? A/B ? C/D evaluate A*D ? B*C= both integers, so now compare. The rational arithmetic package will do th= ose two multiplications, then throw the results away. Unfortunately, the f= ragments left on the heap will be too small for any of the results in the n= ext iteration. This is probably what is running you out of memory. You could try increasing the heap size. Set your paging file to some large= number, and try again with heap size set approximately to your main memory= size. You do not want to actually use that pagefile, set it to 3x you mai= n memory size just to avoid system crashes. On Windows, allowing the syste= m to grow the pagefile, or putting it on C: are both bad news. A cheap thu= mb drive will do nicely, and you don't have to worry about backing it up. A much better idea is to throw away the limit, and have your program print = the approximation at every step. I don't know what you are looking for her= e, but a dozen iterations should give you all the accuracy you might need. = (Even if you are measuring a diagonal brace for a house the size of the uni= verse.)