[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[newbie question] How to compare boolean expression/results?
From: |
Carlos Nepomuceno |
Subject: |
[newbie question] How to compare boolean expression/results? |
Date: |
Tue, 4 Jun 2013 01:52:49 +0300 |
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?
- [newbie question] How to compare boolean expression/results?,
Carlos Nepomuceno <=