comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Ada function sqrt(x)
Date: 1996/09/22
Date: 1996-09-22T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023080002209961735440001@news.ni.net> (raw)
In-Reply-To: Dy0xBw.A1u@thomsoft.com


> >I want to find the sqrt of a interger... how can I do this and what package
>> >do I need to declare
>[...]
>> If you are using Ada95 then the package is
>> 
>> Ada.Numerics.Generic_Elementary_Functions or
>> Ada.Numerics.Elementary_Functions (for a float instanciation).
>
>The original question was about finding the square root of an *integer*;
>Generic_Elementary_Functions and its instantiations work on floating-point
>types.

If you really want a function to do integer square roots, here it is (my
Ada 95 syntax may not be quite right):

   generic
      type T is range <>;
   function Generic_Square_Root (N : T) return T'Base;

   function Generic_Square_Root (N: T) return T'Base is
      The_Square_Root : T'Base := N/2;
   begin
      loop
         declare
            The_New_Value : constant T'Base := 
               (The_Square_Root + N / The_Square_Root) / 2;
         begin
            exit when The_New_Value = The_Square_Root;
         end;
      end loop;

      return The_Square_Root;
   end Generic_Square_Root; 

This will work in Ada 83, too, by removing the references to 'Base and
making sure type T has a large enough range to include the value of the
square root.

matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  parent reply	other threads:[~1996-09-22  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <01bba50c$7c1a2760$dc014dc6@MountainNet>
1996-09-18  0:00 ` Ada function sqrt(x) Pascal Obry
1996-09-20  0:00   ` Keith Thompson
1996-09-20  0:00     ` Roderick Chapman
1996-09-21  0:00     ` Robert Dewar
1996-09-21  0:00     ` Robert Dewar
1996-09-26  0:00       ` Keith Thompson
1996-09-22  0:00     ` Matthew Heaney [this message]
1996-09-22  0:00       ` Matthew Heaney
1996-09-24  0:00 W. Wesley Groleau (Wes)
replies disabled

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