comp.lang.ada
 help / color / mirror / Atom feed
* Integer Square Root
@ 1996-10-15  0:00 Matthew Heaney
  1996-10-17  0:00 ` Keith Thompson
  1996-10-17  0:00 ` Mats Weber
  0 siblings, 2 replies; 6+ messages in thread
From: Matthew Heaney @ 1996-10-15  0:00 UTC (permalink / raw)




W. Wesley Groleau <mailto:wwgrol@pseserv3.fw.hac.com> 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




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Integer Square Root
@ 1996-10-15  0:00 W. Wesley Groleau (Wes)
  0 siblings, 0 replies; 6+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-10-15  0:00 UTC (permalink / raw)



I meant to post this a long time ago.  Sorry about the delay.

M. Heaney's integer square root algorithm should not be used without testing.
After applying the correction he posted, here's how it performs:

If the input is zero or one, a divide by zero exception is raised.
(constraint_error)

perfect square:
If the input is N*N where N is any integer greater than one, the answer
is correct (after applying the correction you posted).

perfect square minus one:
For the same N, an input of N*N-1 never terminates.

For all other positive integers, the answer is equivalent to the float
answer with the fractional part truncated, i.e., root 62 comes out 7 where
integer(sqrt(float(62))) would be 8.  (Some people may prefer truncation).

For negative numbers, sometimes it produces an answer (possibly a negative
answer), sometimes it doesn't terminate, sometimes it raises the exception.

---------------------------------------------------------------------------
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] 6+ messages in thread

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-10-15  0:00 Integer Square Root Matthew Heaney
1996-10-17  0:00 ` Keith Thompson
1996-10-17  0:00   ` Matthew Heaney
1996-10-22  0:00     ` Oliver Kellogg
1996-10-17  0:00 ` Mats Weber
  -- strict thread matches above, loose matches on Subject: below --
1996-10-15  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