From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f362ea01ca404786 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-29 06:45:20 PST Newsgroups: comp.lang.ada Path: swrinde!hookup!news.mathworks.com!uunet!zib-berlin.de!zrz.TU-Berlin.DE!cs.tu-berlin.de!informatik.uni-bremen.de!marvin.pc-labor.uni-bremen.de!news.uni-stuttgart.de!news.belwue.de!news.informatik.uni-stuttgart.de!spiegel From: spiegel@bruessel.informatik.uni-stuttgart.de (Andre Spiegel) Subject: Re: Easy way to read FLOAT? 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 ] Message-ID: Sender: news@informatik.uni-stuttgart.de Organization: University of Stuttgart, Germany References: <3gaihr$hak@pdq.coe.montana.edu> Date: Sun, 29 Jan 1995 11:49:33 GMT Date: 1995-01-29T11:49:33+00:00 List-Id: 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