Hi Carlos,

You can simplify your code as follows:

:- initialization(main).

commutative(X, Y) :-
    X =:= Y.

main :-
    A = 1,
    B = 1 / (1-2.220446049250313e-16),
    (   commutative(A, B) ->
        write(true)
    ;
        write(false)
    ).


Since Prolog works with predicates (which can then be true or false, i.e. succeed or fail) it is better to avoid the 3rd argument to equal and commutative and to simply state commutative succeeds iff X and Y are commutative. NB: there is no need to check X =:= Y and Y =:= X (=:= is a symetric relation in Prolog). So there is no more need for the predicate equal (use =:= directly).

BTW on my machine with e-16 it is NOT commutative while with e-17 it is.

Daniel

Le 04/06/2013 00:52, Carlos Nepomuceno a écrit :
Hi guys! This is my first post to this list.

I'm not really learning Prolog at the moment but I started to compare how 
different programming languages treat the following floating point comparison 
case:


a=1
b=1/(1-epsilon)
(a==b) == (b==a)

That's basic a test to check if the equality operator ('==') is commutative.

I found it really hard to implement that simple test in Prolog. I've read a lot 
of stuff and the only thing I've found to work (AFAIK) was the following:

%works in GNU Prolog 1.4.0 
(http://www.compileonline.com/execute_prolog_online.php)
:- initialization(main).
equal(X,Y,Z) :- (X=:=Y -> Z=true; Z=false).
commutative(X,Y,Z) :- ((equal(X,Y,W), equal(Y,X,W)) -> Z=true; Z=false).
main :- A = 1,
         B = 1/(1-2.220446049250313e-16),
         commutative(A,B,T),
         write(T).
%output: true

That seems to be very verbose and inefficient solution.

How can I do it simpler? Is it possible to make it faster?                      
                
_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog



--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.


_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog

Reply via email to