comp.lang.ada
 help / color / mirror / Atom feed
* Re: Safely converting String to Float ?
  1999-11-03  0:00   ` Preben Randhol
@ 1999-11-03  0:00     ` Lutz Donnerhacke
  1999-11-03  0:00       ` Preben Randhol
  0 siblings, 1 reply; 14+ messages in thread
From: Lutz Donnerhacke @ 1999-11-03  0:00 UTC (permalink / raw)


* Preben Randhol wrote:
>Aha. Btw. How do one find a overview of these packages. I've been
>looking in the RM, but I haven't really found one (overview). I'm not
>to familiar with using it yet though.

http://www.adahome.com/rm95/rm9x-A.html
Annex A: Predefined Language Environment (normative) 





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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
  1999-11-03  0:00 ` Robert Dewar
  1999-11-03  0:00 ` Robert Dewar
@ 1999-11-03  0:00 ` Matthew Heaney
  1999-11-03  0:00   ` Preben Randhol
  1999-11-03  0:00 ` Gautier
  3 siblings, 1 reply; 14+ messages in thread
From: Matthew Heaney @ 1999-11-03  0:00 UTC (permalink / raw)


In article <m3n1svbw6t.fsf@kiuk0156.chembio.ntnu.no> , Preben Randhol 
<randhol@pvv.org>  wrote:

> begin
>    Get(Tekst,Tall,Size);
>    Get(Tekst2,Tall_F,Size_F);
> end;

Use an exception handler:

  begin
    Get (Tekst, Tall, Size);
  exception
    when Data_Error =>         -- check this in the RM
      Put_Line (<whatever>);
  end;

  begin
    Get (...);
  exception
    when Data_Error =>
     Put_Line (...);
  end;


A few more points:

1) If you're using type Float, you don't need to instantiate
Text_IO.Float_IO, because there's a predefined package called
Ada.Float_Text_IO.


2) If you use Get, then the last parameter is not a "size" of subtype
"Integer," it indicates the last index position of the value image, and
its subtype is "Natural."

  declare
    Image : String := "<image of floating point value>";
    Value : Float;
    Last  : Positive;
  begin
    Get (From => Image, Item => Value, Last => Last);
  exception
    when Data_Error =>
      <handle error>
  end;


3) You don't really need Float_Text_IO or Get; simply use the attribute
Float'Value to convert the string:

  declare
    Image : String := <whatever>;
    Value : Float;
  begin
    Value := Float'Value (Image);
  exception
    when Constraint_Error =>      -- note different exception
      <handle error>
  end;





--
Evolution is as well documented as any phenomenon in science, as
strongly as the earth's revolution around the sun rather than vice
versa.

Stephen Jay Gould, Time, 23 Aug 1999




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
                   ` (2 preceding siblings ...)
  1999-11-03  0:00 ` Matthew Heaney
@ 1999-11-03  0:00 ` Gautier
  3 siblings, 0 replies; 14+ messages in thread
From: Gautier @ 1999-11-03  0:00 UTC (permalink / raw)


> begin
>    Get(Tekst,Tall,Size);
>    Get(Tekst2,Tall_F,Size_F);
> end;

> I would like the program to deal with the failure of
> conversion in a kinder manner.

begin
   Get(Tekst,Tall,Size);
   Get(Tekst2,Tall_F,Size_F);
exception
   when data_error => Put("Your string is wrong, my dear!");
end;

or variants...

HTH
 
Gautier

_____\\________________\_______\_________
http://members.xoom.com/gdemont/gsoft.htm




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
  1999-11-03  0:00 ` Robert Dewar
@ 1999-11-03  0:00 ` Robert Dewar
  1999-11-03  0:00 ` Matthew Heaney
  1999-11-03  0:00 ` Gautier
  3 siblings, 0 replies; 14+ messages in thread
From: Robert Dewar @ 1999-11-03  0:00 UTC (permalink / raw)


In article <m3n1svbw6t.fsf@kiuk0156.chembio.ntnu.no>,
  Preben Randhol <randhol@pvv.org> wrote:

> it will dump core


If this dumps core, you have an installation problem. This
program should raise Data_Error and certainly does when I
run it:

   raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiflau.adb:88

Of course since you have no handler for data error, the
program is terminated. Go study the use of exception handlers
in whatever text or tutorial you are reading, that's what you
need here!


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
@ 1999-11-03  0:00 ` Robert Dewar
  1999-11-03  0:00   ` Preben Randhol
  1999-11-03  0:00 ` Robert Dewar
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 14+ messages in thread
From: Robert Dewar @ 1999-11-03  0:00 UTC (permalink / raw)


In article <m3n1svbw6t.fsf@kiuk0156.chembio.ntnu.no>,
  Preben Randhol <randhol@pvv.org> wrote:

> it will dump core


If this dumps core, you have an installation problem. This
program should raise Data_Error and certainly does when I
run it:

   raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiflau.adb:88

Of course since you have no handler for data error, the
program is terminated. Go study the use of exception handlers
in whatever text or tutorial you are reading, that's what you
need here!


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Safely converting String to Float ?
@ 1999-11-03  0:00 Preben Randhol
  1999-11-03  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Preben Randhol @ 1999-11-03  0:00 UTC (permalink / raw)



How do one safely (no program crash) convert a String to a Float?

This little code will dump core:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;

procedure StrToFloat is

   package My_Float_IO is new
     Ada.Text_IO.Float_IO(Float);
   use My_Float_IO;

   Tekst  : String(1..5) := "12.34";
   Tekst2 : String(1..6) := "d12.34";
   Tall  : Float;
   Size  : Integer;
   Tall_F : Float;
   Size_F : Integer;

begin
   Get(Tekst,Tall,Size);
   Get(Tekst2,Tall_F,Size_F);
end;

it will dump core because of the d in Tekst2. Does somebody know how
to do this conversion without core dumping? I'm not trying to convert
the d12.34, but I would like the program to deal with the failure of
conversion in a kinder manner.

I'm using Gnat 3.12p on a Linux 2.2.x System (Redhat 6.0). And I'm an
Ada newbie.

Any hints much appreciated...

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 ` Matthew Heaney
@ 1999-11-03  0:00   ` Preben Randhol
  1999-11-03  0:00     ` Lutz Donnerhacke
  0 siblings, 1 reply; 14+ messages in thread
From: Preben Randhol @ 1999-11-03  0:00 UTC (permalink / raw)


"Matthew Heaney" <matthew_heaney@acm.org> writes:

| A few more points:
| 
| 1) If you're using type Float, you don't need to instantiate
| Text_IO.Float_IO, because there's a predefined package called
| Ada.Float_Text_IO.

Aha. Btw. How do one find a overview of these packages. I've been
looking in the RM, but I haven't really found one (overview). I'm not
to familiar with using it yet though.

| 2) If you use Get, then the last parameter is not a "size" of subtype
| "Integer," it indicates the last index position of the value image, and
| its subtype is "Natural."

Oops you'r right. Thanks.
 
| 3) You don't really need Float_Text_IO or Get; simply use the attribute
| Float'Value to convert the string:

Ah great now it works! Thank you very much!

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00     ` Lutz Donnerhacke
@ 1999-11-03  0:00       ` Preben Randhol
  1999-11-08  0:00         ` Robert A Duff
  0 siblings, 1 reply; 14+ messages in thread
From: Preben Randhol @ 1999-11-03  0:00 UTC (permalink / raw)


lutz@iks-jena.de (Lutz Donnerhacke) writes:

| * Preben Randhol wrote:
| >Aha. Btw. How do one find a overview of these packages. I've been
| >looking in the RM, but I haven't really found one (overview). I'm not
| >to familiar with using it yet though.
| 
| http://www.adahome.com/rm95/rm9x-A.html
| Annex A: Predefined Language Environment (normative) 

OK. There isn't an overview that only lists the pcakages with links
to the documentation of each package then?

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00 ` Robert Dewar
@ 1999-11-03  0:00   ` Preben Randhol
  1999-11-03  0:00     ` Samuel Tardieu
  0 siblings, 1 reply; 14+ messages in thread
From: Preben Randhol @ 1999-11-03  0:00 UTC (permalink / raw)


Robert Dewar <robert_dewar@my-deja.com> writes:
 
| If this dumps core, you have an installation problem. This
| program should raise Data_Error and certainly does when I
| run it:
| 
|    raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tiflau.adb:88

Hmm. I have only installed the Gnat 3.12p RPM from
http://www.gnuada.org/rpms312p.html

Maybe I forgot to install something.

| Of course since you have no handler for data error, the
| program is terminated. Go study the use of exception handlers
| in whatever text or tutorial you are reading, that's what you
| need here!

Yes, but you'r saying that it should not dump core even if I don't use
exceptions here, right?

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00   ` Preben Randhol
@ 1999-11-03  0:00     ` Samuel Tardieu
  1999-11-04  0:00       ` Jürgen Pfeifer
  1999-11-04  0:00       ` [SOLVED] " Preben Randhol
  0 siblings, 2 replies; 14+ messages in thread
From: Samuel Tardieu @ 1999-11-03  0:00 UTC (permalink / raw)


>>>>> "Preben" == Preben Randhol <randhol@pvv.org> writes:

Preben> Hmm. I have only installed the Gnat 3.12p RPM from
Preben> http://www.gnuada.org/rpms312p.html

You should report this to Juergen Pfeifer <juergen@gnuada.org>, the
GNAT packager for RedHat, since it looks like a problem related to
RedHat (the Debian package produces the expected exception).

Preben> Yes, but you'r saying that it should not dump core even if I
Preben> don't use exceptions here, right?

Yup.
 
  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00     ` Samuel Tardieu
@ 1999-11-04  0:00       ` Jürgen Pfeifer
  1999-11-04  0:00       ` [SOLVED] " Preben Randhol
  1 sibling, 0 replies; 14+ messages in thread
From: Jürgen Pfeifer @ 1999-11-04  0:00 UTC (permalink / raw)


Samuel Tardieu wrote:
> 
> >>>>> "Preben" == Preben Randhol <randhol@pvv.org> writes:
> 
> Preben> Hmm. I have only installed the Gnat 3.12p RPM from
> Preben> http://www.gnuada.org/rpms312p.html
> 
> You should report this to Juergen Pfeifer <juergen@gnuada.org>, the
> GNAT packager for RedHat, since it looks like a problem related to
> RedHat (the Debian package produces the expected exception).
> 
It raises the exception on my box and doesn't core dump. Maybe running
it under control of gnatgdb could provide some more hints whats going
wrong in Prebens environment.

J�rgen




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

* [SOLVED] Re: Safely converting String to Float ?
  1999-11-03  0:00     ` Samuel Tardieu
  1999-11-04  0:00       ` Jürgen Pfeifer
@ 1999-11-04  0:00       ` Preben Randhol
  1 sibling, 0 replies; 14+ messages in thread
From: Preben Randhol @ 1999-11-04  0:00 UTC (permalink / raw)


Samuel Tardieu <sam@ada.eu.org> writes:

| Preben> Yes, but you'r saying that it should not dump core even if I
| Preben> don't use exceptions here, right?
| 
| Yup.

It was indeed a installation problem. I had downloaded and installed
the wrong RPMs. That is the glibc2.0 rpms instead of the glibc2.1.

Thanks everbody for all help.

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: Safely converting String to Float ?
  1999-11-03  0:00       ` Preben Randhol
@ 1999-11-08  0:00         ` Robert A Duff
  1999-11-09  0:00           ` Preben Randhol
  0 siblings, 1 reply; 14+ messages in thread
From: Robert A Duff @ 1999-11-08  0:00 UTC (permalink / raw)


Preben Randhol <randhol@pvv.org> writes:

> OK. There isn't an overview that only lists the pcakages with links
> to the documentation of each package then?

The very first page of Annex A has a list of them.
I don't know if it's got hyperlinks.

Also, look up "language-defined library unit" in the index.

- Bob




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

* Re: Safely converting String to Float ?
  1999-11-08  0:00         ` Robert A Duff
@ 1999-11-09  0:00           ` Preben Randhol
  0 siblings, 0 replies; 14+ messages in thread
From: Preben Randhol @ 1999-11-09  0:00 UTC (permalink / raw)


Robert A Duff <bobduff@world.std.com> writes:

| Also, look up "language-defined library unit" in the index.

That was what I was looking for. Thanks! 

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

end of thread, other threads:[~1999-11-09  0:00 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
1999-11-03  0:00 ` Robert Dewar
1999-11-03  0:00   ` Preben Randhol
1999-11-03  0:00     ` Samuel Tardieu
1999-11-04  0:00       ` Jürgen Pfeifer
1999-11-04  0:00       ` [SOLVED] " Preben Randhol
1999-11-03  0:00 ` Robert Dewar
1999-11-03  0:00 ` Matthew Heaney
1999-11-03  0:00   ` Preben Randhol
1999-11-03  0:00     ` Lutz Donnerhacke
1999-11-03  0:00       ` Preben Randhol
1999-11-08  0:00         ` Robert A Duff
1999-11-09  0:00           ` Preben Randhol
1999-11-03  0:00 ` Gautier

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