Re: Floating point print Incompatibility SWI 5.10.2 versus 6.6.2

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 03/20/2014 06:13 AM, Jeff wrote:
> I have been using the older 5.10.2 for years (on my Intel Mac) and have
> just tried 6.6.2 (on a remote Linux system).
> 
> In 5.10.2:
> 
>  ?-	A is 2.1, write(A).
> 	2.1
> 	A = 2.1.
> 
>  ?-	A is 2.1 + 3.2, write(A).
> 	5.3
> 	A = 5.3. 
> 
> In 6.6.2:
> 
>  ?-	A is 2.1, write(A).
> 	2.1
> 	A = 2.100000000.
> 
>  ?-	A is 2.1 + 3.2, write(A).
> 	5.300000000000001
> 	A = 5.300000000.

I don't know how you get the 0s with A = ...  For me, both
the output of write and A = are the same:

7 ?- A is 2.1 + 3.2, write(A).
5.300000000000001
A = 5.300000000000001.

> I have set flags so that BOTH versions have
> 
> 	current_prolog_flag(float_format, %g’).

This flag doesn't exist anymore.

> and	current_prolog_flag(write_attributes, ignore).

There are no attributes, so, I guess this statement is something similar
as to
say `I tried it on the moon' :-)

> How can I get 6.6.2 to behave the way 5.10.2 did???

Use format/3:

10 ?- A is 2.1 + 3.2, format('~g', [A]).
5.3
A = 5.300000000000001.

Prolog read/write are there to ensure we can serialize and parse terms
consistently,
so write writes with the minimum number of digits to ensure that read
reads the
same float.  I.e., the conjunction below is supposed to be true for
every term F:

?- format(atom(A), '~q', [F]), atom_to_term(A, F2, _), F =@= F2.

e.g.,

2 ?- F is 2.1 + 3.2, format(atom(A), '~q', [F]), atom_to_term(A, F2, _),
F =@= F2.
F = F2, F2 = 5.300000000000001,
A = '5.300000000000001'.

If we used %g (~g):

3 ?- F is 2.1 + 3.2, format(atom(A), '~g', [F]), atom_to_term(A, F2, _),
F =@= F2.
false.

Format/3 is there to write output for humans and possibly other
computing systems.

	Cheers --- Jan

P.s.	The above implies that unquoted write is meaningless in Prolog.  I fear
	that some people will not agree, but in my experience it is more
	frequently a source of confusion than anything useful.
_______________________________________________
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.