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,5b88cd8e97eaef79 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Integer Square Root Date: 1996/10/15 Message-ID: #1/1 X-Deja-AN: 189725590 content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-10-15T00:00:00+00:00 List-Id: W. Wesley Groleau pointed out to me some errors in an algorithm I posted for performing the square root of an integer number without using floating point arithmetic. That I did that off the top of my head, without any testing (neither static nor dynamic), and the fact that that algorithm basically doesn't work, should prove to everyone (especially me) that "off-the-cuff" programming doesn't work! Thank you, Wes, for pointing out to me the errors of my ways. So here, if I may be given a chance to right my wrongs, is a "correct" version: function Square_Root (N : Natural) return Natural is X0 : Natural := N / 2; X1 : Natural := 2; begin case N is when 0 => return 0; when 1 => return 1; when others => while abs (X0 - X1) > 1 loop X0 := (X0 + X1) / 2; X1 := N / X0; end loop; return X0; end case; end Square_Root; This is similar to an algorithm for fixed point types that appears in Section 5.4 of the Ada 83 Rationale. If anyone is interested in a generic version that works for any integer type, then let me know, and I'll post it. matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271