comp.lang.ada
 help / color / mirror / Atom feed
From: "Kevin J. Weise" <kweise@c3i-ccmail.sed.redstone.army.mil>
Subject: Re: how to make a package????
Date: 1996/08/15
Date: 1996-08-15T00:00:00+00:00	[thread overview]
Message-ID: <4uvllm$f35@michp1.redstone.army.mil> (raw)
In-Reply-To: 4uu2kp$32u@news.us.net


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





  reply	other threads:[~1996-08-15  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-08-15  0:00 how to make a package???? Michael W. Hall
1996-08-15  0:00 ` Kevin J. Weise [this message]
1996-08-15  0:00   ` Michael W. Hall
1996-08-15  0:00 ` John Herro
replies disabled

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