Re: Embarrassing Arithmetic behaviour

"j.gressier" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <847993958.135133.1392926414251.JavaMail.www@wwinf8224>
I knew beforehand Richard's answer which I think has already been delivered here with the same didactic ease.

Now, to soothe the pedagogical pressure : I already suspected that swi prolog was not a liar. I have also been suspecting these last 32 years that floating point arithmetic was not real number arithmetic.

I use the C interface of swi to work with Pascal. It all came from this Pascal code (example):

var a,b : double; 
begin
    b := 4.9 * 100000.0 ;
    if b = 490000 then showmessage('true') else showmessage('false') ;    
    a := PL_compare_mine(b)

Pascal will answer 'true'. (unlike swi-prolog, C, Gambit...  and Python which I have just checked)

I thought both languages used 64bits float IEEE754, hence my "embarrassment" !

I stumbled upon the problem when I used PL_put_float and PL_get_float in something like:

(The last two lines are the test, k is calculated by Pascal and r by Swi prolog, you may skip the other lines)

function PL_compare_mine (k : double) : double;
var requete: qid_t;
     solution : integer;
     arg1,arg2: term_t;
     r : double;
begin
   arg1:=PL_new_term_refs(2);
   arg2:=arg1+1  ;

   PL_put_float(arg1,k);
   requete:=PL_open_query(nil,
                                        PL_Q_NODEBUG,PL_predicate(PChar('plcompare'),2,'user'),
                                       arg1);
   solution:=PL_next_solution(requete);
   PL_get_float(arg2, r);
   PL_close_query(requete);
   result := r;
// ------------------ tests  -----------------------------
   showmessage(floattostr(k)+'    '+floattostr(r));
   if k=r then showmessage('true') else showmessage('false');

end;                        

The prolog code which concerns us is something like :

plcompare(_In,Out) :-   Out is 4.9 * 100000.0.

When you run the program you get :

% pascal test : ok

'true'

%  display of both numbers : ok

 490000    490000


% failure when comparing pascal number and swi prolog number

'false'  

There may be a Pascal and jgressier issue there. Something to do with Pascal floatostr function and with a non investigation on my part of the Pascal arithmetic background.

All the best

J.Gressier

> Message du 20/02/14 13:37
> De : "Derek Jones"
> A : "Boris Vassilev"
> Copie à : "Richard A. O'Keefe" , "j.gressier" , "Prolog mailing list"
> Objet : Re: [SWIPL] Embarrassing Arithmetic behaviour
>
> I have to agree with Richard on this. I find it quite strange.
>
> I learned about this in Computer Science A-level 32 years ago… Let alone a degree - where I *also* learned about it - an Electronic Engineering degree at that!
>
> But - then again - I find that people who I teach programming language classes to often do not have pre-requisites either.
>
> General dumbing down of education I think. Focus on Java - That kind of thing… ;-)
>
> Kind regards
>
> Derek.
>
>
> On Feb 20, 2014, at 2:42 AM, Boris Vassilev wrote:
>
> > On Thu, Feb 20, 2014 at 1:06 AM, Richard A. O'Keefe wrote:
> >
> >> How does anyone get through a programming course
> >> without meeting this stuff?
> >>
> >
> > It is quite easy. A degree in Software Engineering, or a degree in
> > Bioinformatics (with all the courses in them), just to name two examples.
> > Or maybe just doing Web-Development?
> > -------------- next part --------------
> > HTML attachment scrubbed and removed
> > _______________________________________________
> > SWI-Prolog mailing list
> > [email protected]
> > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
>
-------------- next part --------------
HTML attachment scrubbed and removed
_______________________________________________
SWI-Prolog mailing list
[email protected]
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.