Re: Embarrassing Arithmetic behaviour
"j.gressier" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <580864590.339287.1393021718175.JavaMail.www@wwinf8226> |
> > 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)
>
> Erk.
>
> So the "Embarrassing Arithmetic behaviour" is
> clearly happening in *Pascal*, not in Prolog.
> Is there a support list for your Pascal system?
>
> I have no idea what Pascal compiler you are using.
> There is no 'double' in either of the ISO Pascal standards.
>
> So I cannot speak with authority here about your Pascal
> system. Let me tell you about Ada instead.
I use FreePascal ( http://www.freepascal.org/ with Lazarus which is a cross-platform IDE for Rapid Application Development. Both teams have done an amazing job).
Most of my code is in Swi Prolog. Freepascal is used to design the GUI.
The floating point types are described here.
http://www.freepascal.org/docs-html/prog/progsu157.html
>
> In Ada, 4.9 is not a floating point literal,
> it is a literal of type 'Universal_Fixed' with (in principle)
> unbounded precision. Similarly, 100000.0 is also an
> (in principle) infinitely precise number. The expression
> 4.9 * 100000.0 is therefore *not* evaluated with the
> floating-point * but with the Universal_Fixed * at compile
> time, yielding the (in principle) infinitely precise
> 490000. When the compiler notices that the Universal_Fixed
> constant is being assigned to a double precision floating
> point variable, it heaves a sigh of relief (unbounded
> precise arithmetic being _only_ a compile-time feature, not
> a run-time one) and at that point converts the number to
> floating point. And 490000 *is* exactly representable as
> a floating point number.
>
> I cannot say for sure, but if you did
>
> var
> a, b: double;
> begin
> b := 4.9;
> a := 100000.0;
> b := b * a;
> a := 490000.0;
> if b = a then
> showmessage('true');
> else
> showmessage('false');
> end;
>
> you might well get a different answer.
> You certainly would in Ada.
You were right.
I got 'false' this time. It is indeed a Pascal compiler issue.
All the best.
J.Gressier
-------------- next part --------------
HTML attachment scrubbed and removed
_______________________________________________
SWI-Prolog mailing list
[email protected]
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog