comp.lang.ada
 help / color / mirror / Atom feed
* Float -> String conversion?
@ 1997-05-14  0:00 Dale Stanbrough
  1997-05-16  0:00 ` Michel Gauthier
  0 siblings, 1 reply; 3+ messages in thread
From: Dale Stanbrough @ 1997-05-14  0:00 UTC (permalink / raw)



There are two ways that i know of that you can convert from a float
to a string.

	Float'image  - which is in x.xxxxExx format
	Ada.Float_Text_IO.Put

...but Text_IO is a rather large beast, and many people say
that 'no one ever really uses text_io for _real_ work'.

If this is the case, then how could one portably convert a float
to a string?

Dale




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Float -> String conversion?
  1997-05-14  0:00 Float -> String conversion? Dale Stanbrough
@ 1997-05-16  0:00 ` Michel Gauthier
  1997-05-24  0:00   ` Robert Dewar
  0 siblings, 1 reply; 3+ messages in thread
From: Michel Gauthier @ 1997-05-16  0:00 UTC (permalink / raw)



In article <5lbj80$k4p$1@goanna.cs.rmit.edu.au>, Dale Stanbrough
<dale@goanna.cs.rmit.EDU.AU> wrote:

>>  There are two ways that i know of that you can convert from a float
>>  to a string.
>>  
>>          Float'image  - which is in x.xxxxExx format
>>          Ada.Float_Text_IO.Put
>>  
>>  ...but Text_IO is a rather large beast, and many people say
>>  that 'no one ever really uses text_io for _real_ work'.
>>  
>>  If this is the case, then how could one portably convert a float
>>  to a string?

I agree with the idea that Ada.text_IO is harmful :
 - device-dependent,
 - didactically obsolete,
 - mixing of disk keeping and user interface.
 - using disk files implies opening 'standard' files.

To work around the problem, I systematically add to all value
(number-like) types a package for string<-> value conversion
(the specification mimics predefined attributes and Text_IO
subprograms) :

generic

    type ITEM_TYPE is digits <> ;

package STRINGS_FOR_FLOAT is

    function WIDTH return POSITIVE ;
    function IMAGE ( FROM : ITEM_TYPE ) return STRING ;
    function VALUE ( FROM : STRING ) return ITEM_TYPE ;
    procedure PUT ( TO   :     out STRING    ;
                    ITEM : in      ITEM_TYPE ) ;
    procedure GET ( FROM : in      STRING    ;
                    ITEM :     out ITEM_TYPE ;
                    LAST :     out POSITIVE  ) ;

end STRINGS_FOR_FLOAT ;

and a package for request-to-user operations :

generic

    type FLOAT_TYPE is digits <> ;

package REQUESTS . FOR_FLOAT is

    function REQUEST_A_FLOAT
              ( PROMPT      : STRING ) return FLOAT_TYPE ;

    function REQUEST_A_FLOAT
              ( LOWER_BOUND : FLOAT_TYPE ;
                UPPER_BOUND : FLOAT_TYPE ;
                PROMPT      : STRING ) return FLOAT_TYPE ;

    function REQUEST_A_FLOAT
              ( PROMPT      : STRING ;
                EMPTY_LINE  : FLOAT_TYPE ) return FLOAT_TYPE ;

    function REQUEST_A_FLOAT
              ( LOWER_BOUND : FLOAT_TYPE ;
                UPPER_BOUND : FLOAT_TYPE ;
                PROMPT      : STRING ;
                EMPTY_LINE  : FLOAT_TYPE ) return FLOAT_TYPE ;

    generic
        with function CONTROL ( APPLIED_TO : FLOAT_TYPE ) return BOOLEAN ;
        CONTROL_MESSAGE : STRING ;
    function REQUEST_A_FLOAT_WITHOUT_DEFAULT
              ( LOWER_BOUND : FLOAT_TYPE := FLOAT_TYPE ' FIRST ;
                UPPER_BOUND : FLOAT_TYPE := FLOAT_TYPE ' LAST ;
                PROMPT      : STRING ) return FLOAT_TYPE ;

    generic
        with function CONTROL ( APPLIED_TO : FLOAT_TYPE ) return BOOLEAN ;
        CONTROL_MESSAGE : STRING ;
    function REQUEST_A_FLOAT_WITH_DEFAULT
              ( LOWER_BOUND : FLOAT_TYPE := FLOAT_TYPE ' FIRST ;
                UPPER_BOUND : FLOAT_TYPE := FLOAT_TYPE ' LAST ;
                PROMPT      : STRING ;
                EMPTY_LINE  : FLOAT_TYPE ) return FLOAT_TYPE ;

end REQUESTS . FOR_FLOAT ;

I planned also an any-device specification, but could not find time to
complete coding it.

I have similar packages for all classes of predefined numeric types, and for
calendars, polynomials, sets,...  (Requests is often a child of *.Strings in
a generic context)

Both specifications do not depend on Ada.Text_IO and are 
device-independent. Any relevant body can be coded, notably
Requests for window-systems or Strings with different image or parser.

Additionally, it would be useful to have a package containing only the
part of Text_IO with File_Type parameters and no predefined 'file'.

Hope this helps. Any comment appreciated.

----------          ----------          ----------          ---------- 
Michel Gauthier / Laboratoire d'informatique
123 avenue Albert Thomas / F-87060 Limoges
telephone + 33 5 55 43 69 73
fax +33 5 55 43 69 77
----------          ----------          ----------          ----------
Si l'an 2000 est pour vous un mysticisme stupide, utilisez la base 9
If you feel year 2000 a stupid mystic craze, use numeration base 9
----------          ----------          ----------          ----------




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Float -> String conversion?
  1997-05-16  0:00 ` Michel Gauthier
@ 1997-05-24  0:00   ` Robert Dewar
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Dewar @ 1997-05-24  0:00 UTC (permalink / raw)



Dale said

>>  There are two ways that i know of that you can convert from a float
>>  to a string.
>>
>>          Float'image  - which is in x.xxxxExx format
>>          Ada.Float_Text_IO.Put
>>
>>  ...but Text_IO is a rather large beast, and many people say
>>  that 'no one ever really uses text_io for _real_ work'.
>>
>>  If this is the case, then how could one portably convert a float
>>  to a string?

Michel replies

 I agree with the idea that Ada.text_IO is harmful :
  - device-dependent,
  - didactically obsolete,
  - mixing of disk keeping and user interface.
  - using disk files implies opening 'standard' files.

 To work around the problem, I systematically add to all value
 (number-like) types a package for string<-> value conversion
 (the specification mimics predefined attributes and Text_IO
 subprograms) :

Robert notes

  I don't understand what Michel means in his criticisms, Text_IO is
  not device dependent at the abstract level, and there is nothing
  about disk files.

  And more importantly neither of these has the slightest bit of
  relevance to the Put to a string which is what Dale was talking
  about, which has nothing to do with devices or disks.

  Dale, it is perfectly reasonable to use Text_IO for this purpose,
  use Put to a string, formatting things however you like.

  As for "didactically obsolete", I can't even guess what that might
  mean. And as for "mixing of disk keeping and user interface", I
  do not see that Text_IO has anything to do with either, but maybe
  that is just a language problem (disk keeping really doesn't mean
  anything in English!) Perhaps the "didactically" is also a language
  problem. Didactically means to do with teaching, and the purpose
  of Text_IO has nothing to do with teaching, it is for outputting
  text!





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-05-24  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-05-14  0:00 Float -> String conversion? Dale Stanbrough
1997-05-16  0:00 ` Michel Gauthier
1997-05-24  0:00   ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox