Subject: Re: [SWIPL] How to convert atoms to reals

From: Jan Wielemaker <[email-masked]>
Date: Tue, 22 May 2001 20:12:45 +0200

> I am sorry for taking your time with such simple questions.

Maybe better to subscribe and post this type of questions to the
[email-masked].  Real Prolog users generally don't like GUI :-)

> However, I am trying to convert code from another prolog interpreter,
> and I am still a newbie in SwiProlog and xpce. Althought I spend several
> hours searching in the users guide for predicates that could resolve me
> this question and I did not succeed.
> 
> So please tell me how can I convert from an atom to a real to preform
> later some calculations? Or where can I find this answers in the Users
> Guide.

There are some paragraphs devoted to type-conversion in the XPCE
User Guide if I recall well.  Searching for `convert' should do the
trick.

> Thanks in advance for any help you can provide,
> 
> João
> 
> Example for the XPCE code,
> 
> ask_number:-
>     new(Dialog, dialog('Ask for a Real')),
>     send(Dialog, append, new(Fi, text_item('Float'))),
>     send(Dialog, append, button(ok, and(message(@prolog,  print_floatn,
> Fi?selection)   ,
> 
> message(Dialog, destroy))
>              )                                       ),
>     send(Dialog, open).
> 
> print_floatn(X):- /* X should be a float */
>    Y is X * 1.1, nl, nl,
>    write('X x 1.1 = ') , write(Y).

There are three ways out:

	* send(Fi, type, real).

Text-items can be send a ->type.  If they have that, an attempt to get
the selection calls the <-convert method of the type.  If this fails 
it raises a warning, normally causing the item to `flash'.

	* Use get(@pce, convert, Old, Type, New):

	?- get(@pce, convert, '1.1', real, X).
	X = 1.1

	* Use native Prolog predicates:
	
	?- atom_codes('1.1', Codes),
	   number_codes(X, Codes).
	X = 1.1

	Cheers --- Jan