From: gauthier@alphainfo.unilim.fr (Michel Gauthier)
Subject: Re: Float -> String conversion?
Date: 1997/05/16
Date: 1997-05-16T00:00:00+00:00 [thread overview]
Message-ID: <gauthier-1605971054200001@193.50.185.13> (raw)
In-Reply-To: 5lbj80$k4p$1@goanna.cs.rmit.edu.au
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
---------- ---------- ---------- ----------
next prev parent reply other threads:[~1997-05-16 0:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-05-14 0:00 Float -> String conversion? Dale Stanbrough
1997-05-16 0:00 ` Michel Gauthier [this message]
1997-05-24 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox