🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1047

Re: HELP!! Need a square root function/predicate in Prolog

#1047
From: R. Kym Horsell
Date: Wed, 08 Aug 1990 03:26
22 lines
1228 bytes
In article <1990Aug08.001233.6587@hoss.unl.edu> sanjiv@fergvax.unl.edu (Sanjiv K. Bhatia) writes:
>Hi there:
>
>I am working on some experiments using Prolog and need a square root function.
>Will some kind soul mail me a copy of the same if you happen to have one handy?

A suitable routine might use the Newton-Raphson method as follows
(assuming all appropriate floating-point convensions):

sqrt(X,X) :- X>=0, X=<1, !.
sqrt(X,Y) :- X>1, X2 is X/2, eps(EPS), nr(EPS,X,X,X2,Y).

nr(EPS,X,G1,G2,Y) :- abs(G1-G2,T), T<EPS, !, Y is (G1+G2)/2.
nr(EPS,X,G1,G2,Y) :- G is 0.5*(G2+X/G2), nr(EPS,X,G2,G,Y).

abs(X,X) :- X>=0, !.
abs(X,Y) :- X<0, Y is -X.

eps(1.0e-6).

-Kym
===
Technical Details
Message-ID: <3786@bingvaxu.cc.binghamton.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!rpi!leah!bingvaxu!vu0310
References: <1990Aug08.001233.6587@hoss.unl.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!rpi!leah!bingvaxu!vu0310
From: vu0310@bingvaxu.cc.binghamton.edu (R. Kym Horsell)
Newsgroups: comp.lang.prolog
Subject: Re: HELP!!  Need a square root function/predicate in Prolog
Message-ID: <3786@bingvaxu.cc.binghamton.edu>
Date: 8 Aug 90 03:26:13 GMT
References: <1990Aug08.001233.6587@hoss.unl.edu>
Reply-To: vu0310@bingvaxu.cc.binghamton.edu.cc.binghamton.edu (R. Kym Horsell)
Organization: SUNY Binghamton, NY
Lines: 22