comp.lang.ada
 help / color / mirror / Atom feed
* Easy way to read FLOAT?
@ 1995-01-27 10:39 Aaron Diesen
  1995-01-27 14:38 ` Norman H. Cohen
  1995-01-29 11:49 ` Andre Spiegel
  0 siblings, 2 replies; 3+ messages in thread
From: Aaron Diesen @ 1995-01-27 10:39 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1017 bytes --]

Hi-

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).  Do I need to make an 'exception', or is 
there another way to get around this?  (A new type that includes all the 
integers and all the floats?)  I know I should probably make it a string, 
for safety purposes, but I don't think the teacher wants that.  BTW, I could 
not find a FAQ, so don't flame me.

                    .end.
� MUD �                               � MUD �       
-------                               -------
�                  e-m�il:                  � 
�     icsjp318@gemini.oscs.montana.edu      �
�                                           �
�      Other accounts you can e-mail        �
� or finger if you want to waste your time: �
�      icsia110@msu.oscs.montana.edu        �
�       gad7822@msu.oscs.montana.edu        �
---------------------------------------------





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

* Re: Easy way to read FLOAT?
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Norman H. Cohen @ 1995-01-27 14:38 UTC (permalink / raw)


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).

The Get procedure provided by instances of Text_IO.Float_IO in Ada 83
only accepts real literals optionally preceded by a '+' or '-'.  A real
literal always includes a decimal point--er, radix point--with at least
one digit on each side of it.

The Get procedure provided by instances of Ada.Text_IO.Float_IO in Ada 95
is more liberal, allowing the sames forms that real input in Fortran
allows, including numbers without decimal points and numbers with digits
on only one side of a decimal point.

Your alternatives are: 

   - Read raw characters and compute the Float value yourself.  (It's
        deceptively hard to do this right.)
   - Use an Ada 95 compiler (in which the new rules are fully
        implemented)
   - Change your input file to conform to the Ada-83 rules.

I suggest you ask your instructor what he or she has in mind.

--
Norman H. Cohen    ncohen@watson.ibm.com



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

* Re: Easy way to read FLOAT?
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Andre Spiegel @ 1995-01-29 11:49 UTC (permalink / raw)


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



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

end of thread, other threads:[~1995-01-29 11:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox