comp.lang.ada
 help / color / mirror / Atom feed
* Re: how to make a package????
  1996-08-15  0:00 how to make a package???? Michael W. Hall
@ 1996-08-15  0:00 ` Kevin J. Weise
  1996-08-15  0:00   ` Michael W. Hall
  1996-08-15  0:00 ` John Herro
  1 sibling, 1 reply; 4+ messages in thread
From: Kevin J. Weise @ 1996-08-15  0:00 UTC (permalink / raw)



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





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

* Re: how to make a package????
  1996-08-15  0:00 ` Kevin J. Weise
@ 1996-08-15  0:00   ` Michael W. Hall
  0 siblings, 0 replies; 4+ messages in thread
From: Michael W. Hall @ 1996-08-15  0:00 UTC (permalink / raw)



I got the help I needed, so there is no need to reply again unless you 
just want to.





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

* how to make a package????
@ 1996-08-15  0:00 Michael W. Hall
  1996-08-15  0:00 ` Kevin J. Weise
  1996-08-15  0:00 ` John Herro
  0 siblings, 2 replies; 4+ messages in thread
From: Michael W. Hall @ 1996-08-15  0:00 UTC (permalink / raw)



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;





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

* Re: how to make a package????
  1996-08-15  0:00 how to make a package???? Michael W. Hall
  1996-08-15  0:00 ` Kevin J. Weise
@ 1996-08-15  0:00 ` John Herro
  1 sibling, 0 replies; 4+ messages in thread
From: John Herro @ 1996-08-15  0:00 UTC (permalink / raw)



mhall59@us.net (Michael W. Hall) asks about
converting a Sqrt function to a package.
     Your Sqrt function is excellent as it stands.  You wouldn't *convert*
it to a package, but you would *move* it into a new package.  That way,
other programs besides your Square_Root could make use of your excellent
Sqrt function.  Here's how to move your function into a new package:

package Math is  -- This is the package specification.
   function Sqrt (Value : in Float) return Float;
   -- You could add other subprogram specifications here.
end Math;

package body Math is  -- This is the package body.
   function Sqrt (Value : in Float) return Float is
   ...
   end Sqrt;
   -- The bodies of any other subprograms would go here.
end Math;

with Text_IO, Math;  -- Your main program.
procedure Square_Root is
   package Float_IO is new Text_IO.Float_IO (Num => Float);
begin
   Float_IO.Put (Math.Sqrt (16.0));
   Text_IO.New_Line;
   Float_IO.Put (Math.Sqrt (15.0));
   Text_IO.New_Line;
end Square_Root;

     You must compile the package specification first, but you can compile
either the package body or the main program second.  Most compilers will
let you put all three parts into one file and compile that, but the
package specification must come first.
     Now other programs that need Sqrt can "with" your Math package and
gain access to Sqrt.
     There's much more to packages.  For example, the above package can be
improved by using "is separate."  Packages can also help contain the
effects of certain changes, so they affect only a small part of your
program.
     For more information, you can download my Ada Tutor program from the
Web or FTP site below my signature.  Besides more information on packages,
it has a sample Sqrt function similar to yours, but generic so that it can
be used with other floating point types besides the standard Float.
     Also, feel free to ask more questions here.  You'll find the people
in comp.lang.ada eager to help.

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




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

end of thread, other threads:[~1996-08-15  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-15  0:00 how to make a package???? Michael W. Hall
1996-08-15  0:00 ` Kevin J. Weise
1996-08-15  0:00   ` Michael W. Hall
1996-08-15  0:00 ` John Herro

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