From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,afad8ee36624c836,start X-Google-Attributes: gid103376,public From: "W. Wesley Groleau (Wes)" Subject: Re: Ada function sqrt(x) Date: 1996/09/24 Message-ID: <9609242156.AA13710@most>#1/1 X-Deja-AN: 185203757 sender: Ada programming language comments: Gated by NETNEWS@AUVM.AMERICAN.EDU mailer: Elm [revision: 70.85] newsgroups: comp.lang.ada Date: 1996-09-24T00:00:00+00:00 List-Id: Matthew Heaney 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 ---------------------------------------------------------------------------