comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Safely converting String to Float ?
Date: 1999/11/03
Date: 1999-11-03T00:00:00+00:00	[thread overview]
Message-ID: <38203f2a_1@news1.prserv.net> (raw)
In-Reply-To: m3n1svbw6t.fsf@kiuk0156.chembio.ntnu.no

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




  reply	other threads:[~1999-11-03  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-03  0:00 Safely converting String to Float ? Preben Randhol
1999-11-03  0:00 ` Matthew Heaney [this message]
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
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       ` [SOLVED] " Preben Randhol
1999-11-04  0:00       ` Jürgen Pfeifer
1999-11-03  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