Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #52948 > unrolled thread

:Re: Question about a numerical computation

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2016-11-29 09:31 +0000
Last post2016-11-29 09:31 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.forth


Contents

  :Re: Question about a numerical computation "Robert L." <No_spamming@noWhere_7073.org> - 2016-11-29 09:31 +0000

#52948 — :Re: Question about a numerical computation

From"Robert L." <No_spamming@noWhere_7073.org>
Date2016-11-29 09:31 +0000
Subject:Re: Question about a numerical computation
Message-ID<o1jhtt$kgc$1@gioia.aioe.org>
Paul Foley wrote:

> Forget epsilon; just look to see whether tmp changed at all.  When you
> run out of bits, it won't change:
> 
>   (defun heron-sqrt (x)
>     (let ((val 1l0))
>       (loop
>         (let ((next (* 0.5l0 (+ val (/ x val)))))
>           (if (= next val)
>               (return val)
>               (setq val next))))))

Ruby:

def heron_sqrt x
  Enumerator.new{|y| guess = 1
    loop{y.yield guess; guess = (x.fdiv(guess) + guess) * 0.5}
  }.each_cons(2).find{|a,b| a==b}.first
end

(2..22).each{|n| p [n, heron_sqrt(n)]}
[2, 1.414213562373095]
[3, 1.7320508075688772]
[4, 2.0]
[5, 2.23606797749979]
[6, 2.449489742783178]
[7, 2.6457513110645907]
[8, 2.82842712474619]
[9, 3.0]
[10, 3.162277660168379]
[11, 3.3166247903554]
[12, 3.4641016151377544]
[13, 3.6055512754639896]
[14, 3.7416573867739413]
[15, 3.872983346207417]
[16, 4.0]
[17, 4.123105625617661]
[18, 4.242640687119286]
[19, 4.358898943540673]
[20, 4.47213595499958]
[21, 4.58257569495584]
[22, 4.69041575982343]

In Forth?


-- 
In Sweden one liberal newspaper editor responded to the latest polling triumphs
by the ... Sweden Democrats party by saying that he would be happy to flood
Sweden with Islamic State fighters to punish the ... electorate....  [M]ost
media in Sweden is Jewish-owned....
theoccidentalobserver.net/2015/11/douglas-murrays-warning-to-the-jewish-community

[toc] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web