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!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.ada Subject: Re: Fun with Unbounded Rational Numbers Date: Sun, 09 Apr 2017 00:15:25 -0700 Organization: A noiseless patient Spider Message-ID: <87zifq831u.fsf@nightsong.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b16989435b88b0831ddb8cc29a2fc759"; logging-data="10340"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oM+l37fG9ypsYGQUJJr86" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:PCby9y7vwxOo6INgMFtJAkQXry4= sha1:mHoTms/KRUz4oqjDQ0/ADeYNPMQ= Xref: news.eternal-september.org comp.lang.ada:46546 Date: 2017-04-09T00:15:25-07:00 List-Id: "Jeffrey R. Carter" writes: > For example, trying to calculate sqrt(2)... > taking a Long Time (it does conclude eventually). Something seems wrong with that calculation or implementation. I did a simple Haskell version and it did 10 iterations (resulting in numbers of around 750 digits) in almost no time: {-# LANGUAGE BangPatterns #-} import Data.Ratio type RI = Ratio Integer msqrt :: RI -> RI msqrt x = go (3/2) 10 where go g 0 = g go !g fuel = let y = g*g - x in go (g - y/(2*g)) (fuel-1) main = print . msqrt $ 2 ("fuel" is the number of iterations). With 5 iterations it gets the more manageable ratio: 1572584048032918633353217 % 1111984844349868137938112 which is approximately: x = 1.414213562373095 where |x*x - 2| is about 4.44e-16. did you remember to recompute Y in your loop? i.e. M := Two * X; Y = X * X - 2 -- <---- this might have been missing? X := X - Y / M;