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,622fd11f5eb54423 X-Google-Attributes: gid103376,public From: "Kevin J. Weise" Subject: Re: how to make a package???? Date: 1996/08/15 Message-ID: <4uvllm$f35@michp1.redstone.army.mil> X-Deja-AN: 174383879 references: <4uu2kp$32u@news.us.net> content-type: text/plain; charset=us-ascii organization: Redstone Arsenal, Alabama mime-version: 1.0 newsgroups: comp.lang.ada x-mailer: Mozilla 1.22 (Windows; I; 16bit) Date: 1996-08-15T00:00:00+00:00 List-Id: mhall59@us.net (Michael W. Hall) wrote: >I took a beginners course on ADA and now I am trying to learn some on my >own with book I bought, but there seems to be a big gap in the info >between the 2. I need to know how to make a package. I guess that will >allow me to put it somewhere without having to repeat code or whatever. >But at any rate I dont understand at all what Im doing. I have taken >this little square root procedure and was wondering if someone can tell >me what changes need to be made. The only thing I need to limit is >negative numbers. And I guess somehow I need to pass this tolerance to >the package so it can be variable. At any rate if anyone here knows what >Im talking about, can you take a look at it. There must be some simple >way to change a procedure to a package. Thanks. mhall59@us.net > > >--------------------------- > >with Text_IO; > >procedure Square_Root is > package Float_IO is new Text_IO.Float_IO (Num => Float); > > function Sqrt (Value : in Float) return Float is > > Tolerance : constant := 0.000001; > Approx : Float; > > begin > Approx := Value / 2.0; > Calculate_Square_Root: > loop > exit Calculate_Square_Root when abs(Approx ** 2 - Value) < >Tolerance; > Approx := 0.5 * (Approx + Value / Approx); > end loop Calculate_Square_Root; > return Approx; > end Sqrt; >begin > Float_IO.Put (Sqrt (16.0)); > Text_IO.New_Line; > Float_IO.Put (Sqrt (15.0)); > Text_IO.New_Line; >end Square_Root; > Gee, where do I start? First off, it appears there are a few misconceptions here. I can't figure out why you want to convert a subprogram into a package. There is a fundamental difference between subprograms and packages: subprograms are the elementary execution units, packages are containers for (logically-related, we hope) language entities, which could include but is not restricted to subprograms. So, one would write code to *invoke* a subprogram (since it is an executable entity), but one cannot write code to invoke a package (since it is not an executable entity). Presuming you want to preserve the functional part of your procedure, you don't want to *convert* it into a package; but you may wish to make it a part of a package (usually along with other things logically-related to your procedure). Thus, I would expect to see something like: ------------------------------------------------------- package Math_Fcns is ... function Sqrt (Value : in Float) return Float; ... end Math_Fcns; ------------------------------------------------------- package body Math_Fcns is ... function Sqrt (Value : in Float) return Float is Tolerance : constant := 0.000001; Approx : Float; begin Approx := Value / 2.0; Calculate_Square_Root: loop exit Calculate_Square_Root when abs(Approx ** 2 - Value) < Tolerance; Approx := 0.5 * (Approx + Value / Approx); end loop Calculate_Square_Root; return Approx; end Sqrt; ... end Math_Fcns; ------------------------------------------------------- Now, all you need to do in your main program is to tell the compiler that your program is going to use something out of this package. Its just the same as for Text_IO. ------------------------------------------------------- with Text_IO; with Math_Fcns; procedure Square_Root is package Float_IO is new Text_IO.Float_IO (Num => Float); begin Float_IO.Put (Math_Fcns.Sqrt (16.0)); Text_IO.New_Line; Float_IO.Put (Math_Fcns.Sqrt (15.0)); Text_IO.New_Line; end Square_Root; ------------------------------------------------------- As for what to do with negative numbers, this is so elementary that I'm going to leave it as an exercise for you. As for my other questions & concerns: 1. what grade did you get in that beginner's Ada course, and what did they teach you? All this should have been covered in it (and early on, too). 2. the algorithm for finding a square root is optimally bad; there are better (i.e., much faster) algorithms, and ... 3. while I understand the concept of allowing the student to learn for him/herself, the capability for computing a square root is something you should be looking for (heck, you should already know where to find it) in other predefined/vendor-provided packages. Hasn't your teacher informed you of this? Kevin J. Weise email: kweise@sed.redstone.army.mil COLSA Corporation voice: (205) 842-9083 Huntsville, AL .. standard disclaimers apply