comp.lang.ada
 help / color / mirror / Atom feed
From: Paul Rubin <no.email@nospam.invalid>
Subject: Re: Fun with Unbounded Rational Numbers
Date: Sun, 09 Apr 2017 00:15:25 -0700
Date: 2017-04-09T00:15:25-07:00	[thread overview]
Message-ID: <87zifq831u.fsf@nightsong.com> (raw)
In-Reply-To: ocaec1$a0p$1@dont-email.me

"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> 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;

  parent reply	other threads:[~2017-04-09  7:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-08 10:37 Fun with Unbounded Rational Numbers Jeffrey R. Carter
2017-04-08 11:19 ` Dmitry A. Kazakov
2017-04-08 14:14 ` Robert Eachus
2017-04-09  0:30 ` antispam
2017-04-09  8:47   ` Jeffrey R. Carter
2017-04-09 19:25     ` antispam
2017-04-10 17:18       ` Jeffrey R. Carter
2017-04-11 21:39         ` antispam
2017-04-09  7:15 ` Paul Rubin [this message]
2017-04-09  8:56   ` Jeffrey R. Carter
2017-04-09 21:18     ` Paul Rubin
2017-04-10 17:08       ` Jeffrey R. Carter
2017-04-10 19:39         ` Paul Rubin
2017-04-09 10:05   ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox