% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File REALSQR2.PL % From Chapter 2 % close_enough/2 and later version of real_square_root/2 close_enough(X,X) :- !. close_enough(X,Y) :- X < Y, Diff is Y-X, Diff < 0.0001. close_enough(X,Y) :- Y < X, close_enough(Y,X). real_square_root(X,nonexistent) :- % Clause 1 nonvar(X), X < 0.0. real_square_root(X,Y) :- nonvar(X), % Clause 2 X >= 0.0, R is sqrt(X), close_enough(R,Y). real_square_root(X,Y) :- nonvar(X), % Clause 3 X > 0.0, R is -sqrt(X), close_enough(R,Y). real_square_root(X,Y) :- nonvar(Y), % Clause 4 Ysquared is Y*Y, close_enough(Ysquared,X).