comp.lang.ada
 help / color / mirror / Atom feed
* Converting Integer / Float to String
@ 2008-07-17  7:49 Alexander Camek
  2008-07-17  8:05 ` anon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Alexander Camek @ 2008-07-17  7:49 UTC (permalink / raw)


Hi List,

is there another way to convert a given String to an Integer or Float type?

I am currently using some code like this:

begin
Value :=Float'Value(Float_String);
exception
when Constraint_Error => Value := 0.0;
end;

For me this is very ugly and a big hack.
Thanks for any idea.

Greetings

Alexander 





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

* Re: Converting Integer / Float to String
  2008-07-17  7:49 Converting Integer / Float to String Alexander Camek
@ 2008-07-17  8:05 ` anon
  2008-07-17  8:15 ` Dmitry A. Kazakov
  2008-07-20  0:28 ` JPWoodruff
  2 siblings, 0 replies; 6+ messages in thread
From: anon @ 2008-07-17  8:05 UTC (permalink / raw)


Another way is to use the "Ada.Float_Text_IO" package. Especially when 
you may normally need the Float_Text_IO package in the program.

Read a integer from a string (buffer) using the "Get" routine:

   procedure Get ( From : in String ;
                    Item : out Num ;
                    Last : out Positive ) ;

but it require an extra variable.

      String_Position : Positive ;
      ...
      Get ( Value,  Float_String, String_Position ) ;
      ...



In <newscache$sc454k$rp5$1@pleione.3soft.de>, "Alexander Camek" <Alexander.Camek@Elektrobit.com> writes:
>Hi List,
>
>is there another way to convert a given String to an Integer or Float type?
>
>I am currently using some code like this:
>
>begin
>Value :=Float'Value(Float_String);
>exception
>when Constraint_Error => Value := 0.0;
>end;
>
>For me this is very ugly and a big hack.
>Thanks for any idea.
>
>Greetings
>
>Alexander 
>
>




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

* Re: Converting Integer / Float to String
  2008-07-17  7:49 Converting Integer / Float to String Alexander Camek
  2008-07-17  8:05 ` anon
@ 2008-07-17  8:15 ` Dmitry A. Kazakov
  2008-07-17  8:32   ` Alexander Camek
  2008-07-20  0:28 ` JPWoodruff
  2 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2008-07-17  8:15 UTC (permalink / raw)


On Thu, 17 Jul 2008 09:49:16 +0200, Alexander Camek wrote:

> is there another way to convert a given String to an Integer or Float type?

Yes, there are many ways.
 
> I am currently using some code like this:
> 
> begin
> Value :=Float'Value(Float_String);
> exception
> when Constraint_Error => Value := 0.0;
> end;
> 
> For me this is very ugly and a big hack.

Certainly it is ugly, because it is not clear why an improperly spelt or a
too large number should magically become 0.0.

The first question is why do you want to convert string to float. This
makes no sense in most cases. (Parsing text is not about string
conversions.) The second question is about the format of the number in the
string, which includes treatment of blank characters, based numbers, signed
numbers and so on. The third question is about error handling, what happens
upon syntax, overflow, underflow, no number, etc errors.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Converting Integer / Float to String
  2008-07-17  8:15 ` Dmitry A. Kazakov
@ 2008-07-17  8:32   ` Alexander Camek
  2008-07-17  9:24     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Alexander Camek @ 2008-07-17  8:32 UTC (permalink / raw)


Hi Dimitry,

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:62haf1069b85$.tb768rmm6vib.dlg@40tude.net...
> On Thu, 17 Jul 2008 09:49:16 +0200, Alexander Camek wrote:
>> I am currently using some code like this:
>>
>> begin
>> Value :=Float'Value(Float_String);
>> exception
>> when Constraint_Error => Value := 0.0;
>> end;
>>
>> For me this is very ugly and a big hack.
>
> Certainly it is ugly, because it is not clear why an improperly spelt or a
> too large number should magically become 0.0.
> The first question is why do you want to convert string to float. This
> makes no sense in most cases. (Parsing text is not about string
> conversions.) The second question is about the format of the number in the
> string, which includes treatment of blank characters, based numbers, 
> signed
> numbers and so on. The third question is about error handling, what 
> happens
> upon syntax, overflow, underflow, no number, etc errors.

First of all, this was only a base to start discussion on it and at that 
point I need some value to initial my float or integer value to get a proper 
value if the string is malformed.
As far as I can tell, the background of the code snipplet is that the string 
is given from an xml file which is parsed by glib xml and there I get all as 
a string.

To your second question. The whitespaces are trimmed and I can only rely on 
the point that there is a correct value in it, if not then the default value 
will be set or at the beginning for initilisation 0.0 is used.

To your third question. That is reason why I posted here. I want to get a 
idea what I have missed in doing my parsing or how to do it a better way. It 
is clear that there is no silver bullet to do that. But what I want is a 
good feeling about how to deal with it, what I have to think about and how 
to deal with it.

Greetings

Alexander





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

* Re: Converting Integer / Float to String
  2008-07-17  8:32   ` Alexander Camek
@ 2008-07-17  9:24     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2008-07-17  9:24 UTC (permalink / raw)


On Thu, 17 Jul 2008 10:32:59 +0200, Alexander Camek wrote:

> First of all, this was only a base to start discussion on it and at that 
> point I need some value to initial my float or integer value to get a proper 
> value if the string is malformed.

I don't think it is a good idea. Malformed document should be reported,
instead of being silently parsed.

> As far as I can tell, the background of the code snipplet is that the string 
> is given from an xml file which is parsed by glib xml and there I get all as 
> a string.

I see. Doesn't Glib's XML parser have a support for numeric parsing? I
mean, if you already use a parser then it is better to use it at full,
instead of adding other code.

> To your second question. The whitespaces are trimmed and I can only rely on 
> the point that there is a correct value in it,

But it can be like this:

<rubbish> 1.0 2 4 </rubbish>

Then what about encoded Unicode characters? Like "&#x1234". Does the parser
translate them? If it does, how does it this for non-Latin-1 characters? I
don't know it, but the parser could produce UTF-8 octets stored in Ada
String, which then would be OK to parse as if it were Latin-1... (:-))

> if not then the default value 
> will be set or at the beginning for initilisation 0.0 is used.

Well, the cases "there is no any number" and "the file contains rubbish"
are different to me. I would not use the default in the second case.

I have a library for parsing:

   http://www.dmitry-kazakov.de/ada/strings_edit.htm

Your sample code could look like:

begin
   Value := Strings_Edit.Floats.Value (Float_String);
exception
   when End_Error => -- No number
      Value := Default;
   when Data_Error => -- Wrong number, XML file is corrupt
       ...
   when Constraint_Error => -- Out of range
      ...
end;

If saturation of the input is OK, then

   Value (Float_String, ToFirst => True, ToLast => True);

that would saturate the input to the nearest bound instead of
Constraint_Error propagation.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Converting Integer / Float to String
  2008-07-17  7:49 Converting Integer / Float to String Alexander Camek
  2008-07-17  8:05 ` anon
  2008-07-17  8:15 ` Dmitry A. Kazakov
@ 2008-07-20  0:28 ` JPWoodruff
  2 siblings, 0 replies; 6+ messages in thread
From: JPWoodruff @ 2008-07-20  0:28 UTC (permalink / raw)


On Jul 17, 1:49 am, "Alexander Camek" <Alexander.Ca...@Elektrobit.com>
wrote:
> Hi List,
>
> is there another way to convert a given String to an Integer or Float type?
>

Perhaps you could use some part of the numeric IO packages I
constructed some years back:

http://www.dmitry-kazakov.de/ada/Numeric-Name-IO.htm

(Dmitry has kindly hosted the distribution of my contribution)

The packages Numeric_IO and Name_IO, together with their children and
support, assist a program to read a user’s input.  The packages are
intended to support numerical computation by providing “Get” and “Put”
procedures for floating numbers and for vectors and matrices of
floating numbers.

The procedures ease an end-user’s burden in preparing inputs for
computational programs.  The rules for input of floating numbers are
relaxed so that program inputs need not conform to the strict Ada
syntax for floating numbers. Facilities allow input either from files
or interactively. Consistent policies throughout all the services
allow programs to address input errors, to prompt the end-user
interactively or to specify optional default values.

Name-directed input can be used to read a file of input data, even
including physical units as defined by Dmitry's Measures_Edit.

Here are example lines from an input file that could be read by your
program that employs these packages:

 Vect := 1.414, 1.732, 2.0 ;
 P  := 14.9 psi ;   -- one standard atmosphere

--
John



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

end of thread, other threads:[~2008-07-20  0:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-17  7:49 Converting Integer / Float to String Alexander Camek
2008-07-17  8:05 ` anon
2008-07-17  8:15 ` Dmitry A. Kazakov
2008-07-17  8:32   ` Alexander Camek
2008-07-17  9:24     ` Dmitry A. Kazakov
2008-07-20  0:28 ` JPWoodruff

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