Path: 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 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=0, !. abs(X,Y) :- X<0, Y is -X. eps(1.0e-6). -Kym ===