comp.lang.ada
 help / color / mirror / Atom feed
From: "W. Wesley Groleau (Wes)" <wwgrol@PSESERV3.FW.HAC.COM>
Subject: Re: Ada function sqrt(x)
Date: 1996/09/24
Date: 1996-09-24T00:00:00+00:00	[thread overview]
Message-ID: <9609242156.AA13710@most> (raw)


Matthew Heaney <mheaney@NI.NET> suggests:

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

Well, perhaps more than syntax.  Suppose I call an instance with a parameter
of 25......

                                                         Iteration

> generic                                                   1   2   3  ...
>   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;                       12
> begin
>   loop
>     declare
>       The_New_Value : constant T'Base :=
>            (The_Square_Root + N / The_Square_Root) / 2;   1   1   1   ...
>     begin
>       exit when The_New_Value = The_Square_Root;         no  no  no   ...
>     end;
>   end loop;
>
>   return The_Square_Root;
> end Generic_Square_Root;

At the risk of being flamed for inefficency:

function Integer_Square_Root ( N : T ) is

  Temp : constant T'Base :=
         T'Base ( Ada.Numerics.Elementary_Functions.Sqrt ( Float (N) ) );

begin

  if Temp * Temp /= N then

     raise Some_Package.Input_Is_Not_An_Integer;

  else

    return Temp;

  end if;

end Integer_Square_Root;

---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Hughes Defense Communications (MS 10-40)                 Home: 219-471-7206
Fort Wayne,  IN   46808                  (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




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

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-24  0:00 W. Wesley Groleau (Wes) [this message]
     [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-26  0:00       ` Keith Thompson
1996-09-21  0:00     ` Robert Dewar
1996-09-22  0:00     ` Matthew Heaney
1996-09-22  0:00       ` Matthew Heaney
replies disabled

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