Rainer Krauskopf <mail(a)krauskopf-rainer.de> wrote:
1. I can define in Prolog "between(+,?,+)" :
between(A,A,B) :- A =< B .
between(A,X,B) :- A < B ,
A1 is A + 1 ,
between(A1,X,B) .
Please DON'T do that.
For the last 20 years, the argument order of between/3 has been
between(+LowerBound, +UpperBound, ?Element). Sample implementation:
between(L, U, X) :-
( integer(L), integer(U) ->
( integer(X) ->
L =< X, X =< U
; var(X) ->
L =< U,
between_aux(L, U, X)
; /* report type failure in X */
)
; var(L), integer(U), integer(X), X > U -> fail
; integer(L), var(U), integer(X), X < L -> fail
; /* diagnose and report type failure or instantiation fault */
/* in L or U */
).
between_aux(L, L, L) :- !.
between_aux(L, _, L).
between_aux(L, U, X) :-
M is 1 + L,
between_aux(M, U, X).
Part of the point of this is that the closure between(L,U) makes
sense as a unary predicate, while *between(L,X) doesn't. However,
it also follows the inputs-before-outputs principle, and is nicely
consistent with numlist(L, U, [L,L+1,...,U]).
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
I wrote:
> (2) Why are the extra digits at the end DIFFERENT from what one gets from
> C?
> (A2.1) Idunno, why ARE they different?
Manuel Carro <mcarro(a)fi.upm.es> replied:
Because printf is not used to generate and print the result.
I'm afraid this misses the point of my question.
The number is 1926/100.0. When I compare the output of
printf("%.18e", 1926/100.0) with the output of (a copy of Dan Bernstein's
implementation of) Taranto's algorithm, I get
taranto(1.926000000000000156e+01) =>
19.260000000000001563194018672220408916473388671875
That being so, a difference between CIAO output and printf()
output may very well indicate an error in whatever method CIAO
uses for binary->decimal conversion.
It may not, of course. Perhaps in converting Dan Bernstein's code
to ANSI C I introduced an error, so that printf() and taranto() are
both wrong in exactly the same way. Perhaps.
At least the question requires more serious examination than it has
so far received.
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
There are two interesting questions about the 1926/100 example.
(1) Why are there extra digits at the end?
(A1.1) Because floating-point arithmetic is imprecise.
(A1.2) Because the default behaviour is to show answers quite precisely.
(Note: it is IMPORTANT to be able to write out a floating point
number, read it back, and get the *SAME* number you started with.
At one time Quintus Prolog got that wrong, VERY embarrassing.
The Scheme standards actually require write/read round-tripping.)
(2) Why are the extra digits at the end DIFFERENT from what one gets from
C?
(A2.1) Idunno, why ARE they different?
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Dear All
The following might help you to understand what is going on.
It comes from a colleague Dr Maurice Cox:
--------------------------------
This interesting example can be "explained" using the IEEE model of
floating point arithmetic.
The exact result of dividing a by b is X = a/b.
Based on the IEEE model of fp arithmetic, the calculated result is x =
(a/b)(1 + e), where |e| <= u, the unit roundoff.
u = 2^{-52} = 2.2e-16 for Intel, Motorola, ...
The error E in x is x - X = (a/b)e.
Thus, |E| <= |a/b|u.
For the example, a = 1926, b = 100, giving |E| <= (19.26)(2.2e-16) =
4.2e-15.
The observed error using CIAO is 2.9e-15 and using C is 1.6e-15, both of
which are consistent with the bound.
This concept of fp error analysis is widely applicable to explain
results and analyse and compare algorithms.
-------------------------------
Best Wishes
Graeme I Parkin
Centre Mathematics and Scientific Computing
National Physical Laboratory
Queens Road
Teddington, Middlesex
TW11 0LW
Tel: +44 20 8943 7104
Fax: +44 20 8977 7091
-------------------------------------------------------------------
This e-mail and any attachments may contain confidential and/or
privileged material; it is for the intended addressee(s) only.
If you are not a named addressee, you must not use, retain or
disclose such information.
NPL Management Ltd cannot guarantee that the e-mail or any
attachments are free from viruses.
NPL Management Ltd. Registered in England and Wales. No: 2937881
Registered Office: Teddington, Middlesex, United Kingdom TW11 0LW.
-------------------------------------------------------------------
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Rainer,
sorry for the delay in answering more extensively.
> I'm interested in Ciao Prolog. You claim that Ciao Prolog does support
> programming with functions. What does this mean? Please be so kind and
> let me have some examples:
> 1. I can define in Prolog "between(+,?,+)" :
> between(A,A,B) :- A =< B .
> between(A,X,B) :- A < B ,
> A1 is A + 1 ,
> between(A1,X,B) .
> I'd prefer to write :
> between(A,A,B) :- A =< B .
> between(A,X,B) :- A < B ,
> between(A + 1, X, B) .
Simply load into Ciao the following file (it works exactly as you have
written it!):
:- module(_,_,[functions]).
between(A,A,B) :- A =< B .
between(A,X,B) :- A < B ,
between(A + 1, X, B) .
> 2. I can define in Prolog "flin(+,-)" :
> flin(X,Y) :- Y is 3*X + 5 .
> I'd prefer to write :
> flin(X) := 3*X + 5 .
Simply add to the previous file:
flin(X) := 3*X + 5 .
And then, query:
?- flin(4,Y).
Y = 17 ?
(in fact, you can even express the "flin(+,-)" formally in the Ciao
assertion language and this will be used by several tools, including
LPdoc).
> 3. I can define in Prolog "fak(+,-)" :
> fak(0,1).
> fak(N,Y) :- N > 0 ,
> N1 is N - 1 ,
> fak(N1,Y1),
> Y is N*Y1 .
> I'd prefer to write :
> fak(0) := 1 .
> fak(N) := N*fak(N - 1) if N > 0 .
Simply add:
fak(0) := 1 .
fak(N) := N* ~fak(N - 1) :- N > 0 .
The ~ is an 'eval' --the opposite of a 'quote'-- and it means that fak
should be called, i.e., that it is not a data structure. You can also
write
:- function fak/1.
fak(0) := 1 .
fak(N) := N* fak(N - 1) :- N > 0 .
to avoid the need to use the ~.
> 4. I can define in Prolog "fib(+,-)" :
> fib(0,0).
> fib(1,1).
> fib(X,Y) :- X > 1 ,
> X2 is X - 2 ,
> X1 is X - 1 ,
> fib(X2,Y2) ,
> fib(X1,Y1) ,
> Y is Y2 + Y1 .
> I'd prefer to write :
> fib(0) := 0 .
> fib(1) := 1 .
> fib(X) := fib(X - 2) + fib(X - 1) if X > 1 .
The only difference is that for consistency we use :- instead of 'if':
:- function fib/1.
fib(0) := 0 .
fib(1) := 1 .
fib(X) := fib(X - 2) + fib(X - 1) :- X > 1 .
or
fib(0) := 0 .
fib(1) := 1 .
fib(X) := ~fib(X - 2) + ~fib(X - 1) :- X > 1 .
> 5. I can define in Prolog "divides(?,+)" :
> divides(X,Y) :- between(1,X,Y) ,
> 0 is Y mod X .
> I can ask the questions :
> ?- X is 3*4 , fak(3,Y) , fak(Y,Z) , divides(X,Z) .
> ?- fak(4,Y) , divides(X,Y) .
> ?- fib(4,X) , Y is 2*4 , fib(Y,Z) , divides(X,Z) .
> ?- fib(6,Y) , fib(Y,Z) , divides(X,Z) .
> I'd prefer to formulate :
> ?- divides( 3*4 , fak(fak(3)) ) .
> ?- divides( X , fak(4) ) .
> ?- divides( fib(4) , fib( 2*4 ) ) .
> ?- divides( X , fib(fib( 6 )) ) .
What you want here is to activate the functional syntax in the top
level. This can be done in the a the natural manner:
?- use_package(functions).
{Including /home/clip/lib/ciao/ciao-1.9/library/functions/functions.pl
{Including /home/clip/lib/ciao/ciao-1.9/library/functions/ops.pl
}
}
yes
?- divides( 3*4 , ~fak(~fak(3)) ) .
yes
?- divides( X , ~fak(4) ) .
X = 1 ?
yes
?- divides( ~fib(4) , ~fib( 2*4 ) ) .
yes
?- divides( X , ~fib(~fib( 6 )) ) .
X = 1 ?
yes
?-
> Is there a context sensitive help for built in predicates?
Yes! Simply do C-c TAB while in the development environment (i.e.,
after opening a .pl file with emacs).
Hope this helps!
The Ciao Development Team
--
-----------------------------------------------------------------------------
herme(a)fi.upm.es | Manuel Hermenegildo
+34-91-336-7435 (Work) | Facultad de Informatica, UPM
+34-91-352-4819 or 336-7412 (FAX) | Universidad Politecnica de Madrid
http://www.clip.dia.fi.upm.es/~herme | 28660-Boadilla del Monte, MADRID SPAIN
-----------------------------------------------------------------------------
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
[We thought this might be of interest to other Ciao users]
------- Start of forwarded message -------
X-Coding-System: nil
Return-Path: <mail(a)krauskopf-rainer.de>
Date: Wed, 02 Apr 2003 18:58:27 +0200
From: Rainer Krauskopf <mail(a)krauskopf-rainer.de>
Subject: functions in Ciao Prolog
To: herme(a)clip.dia.fi.upm.es
MIME-version: 1.0
X-Priority: 3 (Normal)
Dear Manuel,
I'm interested in Ciao Prolog. You claim
that Ciao Prolog does support programming
with functions. What does this mean? Please
be so kind and let me have some examples:
1. I can define in Prolog "between(+,?,+)" :
between(A,A,B) :- A =< B .
between(A,X,B) :- A < B ,
A1 is A + 1 ,
between(A1,X,B) .
I'd prefer to write :
between(A,A,B) :- A =< B .
between(A,X,B) :- A < B ,
between(A + 1, X, B) .
2. I can define in Prolog "flin(+,-)" :
flin(X,Y) :- Y is 3*X + 5 .
I'd prefer to write :
flin(X) := 3*X + 5 .
3. I can define in Prolog "fak(+,-)" :
fak(0,1).
fak(N,Y) :- N > 0 ,
N1 is N - 1 ,
fak(N1,Y1),
Y is N*Y1 .
I'd prefer to write :
fak(0) := 1 .
fak(N) := N*fak(N - 1) if N > 0 .
4. I can define in Prolog "fib(+,-)" :
fib(0,0).
fib(1,1).
fib(X,Y) :- X > 1 ,
X2 is X - 2 ,
X1 is X - 1 ,
fib(X2,Y2) ,
fib(X1,Y1) ,
Y is Y2 + Y1 .
I'd prefer to write :
fib(0) := 0 .
fib(1) := 1 .
fib(X) := fib(X - 2) + fib(X - 1) if X > 1 .
5. I can define in Prolog "divides(?,+)" :
divides(X,Y) :- between(1,X,Y) ,
0 is Y mod X .
I can ask the questions :
?- X is 3*4 , fak(3,Y) , fak(Y,Z) , divides(X,Z) .
?- fak(4,Y) , divides(X,Y) .
?- fib(4,X) , Y is 2*4 , fib(Y,Z) , divides(X,Z) .
?- fib(6,Y) , fib(Y,Z) , divides(X,Z) .
I'd prefer to formulate :
?- divides( 3*4 , fak(fak(3)) ) .
?- divides( X , fak(4) ) .
?- divides( fib(4) , fib( 2*4 ) ) .
?- divides( X , fib(fib( 6 )) ) .
You see I'd appreciate the availability of the functional notation.
What notation is available within Ciao Prolog?
Is there a context sensitive help for built in predicates?
Thank you in advance!
Best regards
R. Krauskopf
------- End of forwarded message -------
--
-----------------------------------------------------------------------------
herme(a)fi.upm.es | Manuel Hermenegildo
+34-91-336-7435 (Work) | Facultad de Informatica, UPM
+34-91-352-4819 or 336-7412 (FAX) | Universidad Politecnica de Madrid
http://www.clip.dia.fi.upm.es/~herme | 28660-Boadilla del Monte, MADRID SPAIN
-----------------------------------------------------------------------------
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Quoting Bart Demoen <Bart.Demoen(a)cs.kuleuven.ac.be>:
> It is perhaps official CIAO mailing list policy to be overpolite and
> cautious in its response, but I am not one of the CIAO officials, so I
> can butt in like an elephant in a porcelano shop ...
You still are subject to the usual conventions of civilised behaviour, though.
I know of floating point arithmetic, but I wanted to hear a proper technical
explanation, particularly concerning the inconsistency of the results and
whether it would be fixed in later versions (I now know it will).
Your input to the discussion was patronising and unnecessary.
--
Wamberto Vasconcelos, PhD wvasconcelos(a)acm.org
Department of Computing Science http://www.csd.abdn.ac.uk/~wvasconc
University of Aberdeen, Aberdeen AB24 3UE, Scotland, UK
Phone +44 (0)1224 272283 Fax +44 (0)1224 273422
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Dear CIAO users (and Jose Morales, in special)
Thanks for your replies so far about my problems with arithmetic in CIAO.
I would just like to add at this point that if I try the same division with
other values, the long sequence of zeros won't appear. Check this:
?- X is 1927/100.
X = 19.27 ?
However, if I try
?- X is 1926/100.
X = 19.2600000000000029 ?
I am running CIAO version 1.8 and the specs of my machine (as requested by Jose
Morales) are:
Operating System
Microsoft Windows XP Professional
Version: 5.1.2600
Memory (RAM)
Capacity: 512 MB
Processor
Intel(R) Pentium(R) 4 CPU 2.26GHz
Version: x86 Family 15 Model 2 Stepping 4
Speed: 2253 MHz
--
Wamberto Vasconcelos, PhD wvasconcelos(a)acm.org
Department of Computing Science http://www.csd.abdn.ac.uk/~wvasconc
University of Aberdeen, Aberdeen AB24 3UE, Scotland, UK
Phone +44 (0)1224 272283 Fax +44 (0)1224 273422
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Folks
One of my students drew my attention to the following unexpected result from
CIAO:
?- X is 1926/100.
X = 19.2600000000000029 ?
Why is the "29" appearing at the end? Is this only happening in our current
version?
--
Wamberto Vasconcelos, PhD wvasconcelos(a)acm.org
Department of Computing Science http://www.csd.abdn.ac.uk/~wvasconc
University of Aberdeen, Aberdeen AB24 3UE, Scotland, UK
Phone +44 (0)1224 272283 Fax +44 (0)1224 273422
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------
Folks
One of my students drew my attention to the following unexpected result from
CIAO:
?- X is 1926/100.
X = 19.2600000000000029 ?
Why is the "29" appearing at the end? Is this only happening in our current
version?
--
Wamberto Vasconcelos, PhD wvasconcelos(a)acm.org
Department of Computing Science http://www.csd.abdn.ac.uk/~wvasconc
University of Aberdeen, Aberdeen AB24 3UE, Scotland, UK
Phone +44 (0)1224 272283 Fax +44 (0)1224 273422
==============================================================================
Message: Address: Action:
help majordomo(a)clip.dia.fi.upm.es Info. on useful commands
subscribe ciao-users-request(a)clip.dia.fi.upm.es Subscribe to this list
unsubscribe ciao-users-request(a)clip.dia.fi.upm.es Unsubscribe from this list
<whatever> ciao-users(a)clip.dia.fi.upm.es Send message to list
-----------------------------------------------------------------------------
Archived messages: http://www.clip.dia.fi.upm.es/Mail/ciao-users/
-----------------------------------------------------------------------------