comp.lang.ada
 help / color / mirror / Atom feed
From: spiegel@bruessel.informatik.uni-stuttgart.de (Andre Spiegel)
Subject: Re: Easy way to read FLOAT?
Date: Sun, 29 Jan 1995 11:49:33 GMT
Date: 1995-01-29T11:49:33+00:00	[thread overview]
Message-ID: <SPIEGEL.95Jan29124933@berlin.bruessel.informatik.uni-stuttgart.de> (raw)
In-Reply-To: icsjp318@gemini.oscs.montana.edu's message of 27 Jan 1995 10:39:55 GMT    [ 27 Jan 1995 11:39:55 MET ]

In article <3gaihr$hak@pdq.coe.montana.edu> icsjp318@gemini.oscs.montana.edu (Aaron Diesen) writes:

> I am working on my first ada program, and I need to read a float.  I ran 
> into problems when trying to input an integer into a float variable 
> (works in other languages). [...] 

Has to do with what I have been working on just recently, it's also been
discussed here in this group. I'm not sure if I understand your
question correctly. Let me take it that you want to read some input and you
don't know whether you are looking at an integer or a float literal.

In your program, the corresponding variable should definitely be a
float (because float values are, roughly speaking, a superset of
integer values). First, you should read your input into a
string. Easiest way to do that: use Text_IO.Get_Line and work with the
resulting string furtheron, let's call it `Line'.

You can then try to interpret the string as a float literal. If that fails,
because it's an integer literal (i.e. there's no decimal point, etc.), 
Data_Error will be raised, and you can give it a second try by trying 
to read it as an integer:

  declare
    Val : Float;
  begin
    Get (Line (1..Last), Val, Index);
  exception
    when Data_Error =>
      declare 
        Int_Val : Integer;
      begin
        Get (Line (1..Last), Int_Val, Index);
        Val := Float (Int_Val);
      exception
        when Data_Error =>
          raise;  -- no, it wasn't an Integer either
      end;
  end;

This fails, of course, for very large "Integer" values, which is the
reason why I wrote that I/O package I talked about the other day. If
large values are a problem for you, send me e-mail.

Have I done someone's homework now? ;-)

Regards,

--
Andre Spiegel                     |  This life is a test. It is only a test.
                                  |  Had this been an actual life, you would
University of Stuttgart, Germany  |  have received further instructions as to
Computer Science                  |  where to go and what to do.
                                                            -- Author unknown

	   email: spiegel@bruessel.informatik.uni-stuttgart.de



      parent reply	other threads:[~1995-01-29 11:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-01-27 10:39 Easy way to read FLOAT? Aaron Diesen
1995-01-27 14:38 ` Norman H. Cohen
1995-01-29 11:49 ` Andre Spiegel [this message]
replies disabled

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