comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada function sqrt(x)
@ 1996-09-24  0:00 W. Wesley Groleau (Wes)
  0 siblings, 0 replies; 9+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-09-24  0:00 UTC (permalink / 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
---------------------------------------------------------------------------




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

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

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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
1996-09-24  0:00 W. Wesley Groleau (Wes)

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