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,c0fe3a04fa2416f1 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Ada function sqrt(x) Date: 1996/09/22 Message-ID: #1/1 X-Deja-AN: 184693452 references: <01bba50c$7c1a2760$dc014dc6@MountainNet> <51o7nh$5vl@cf01> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-09-22T00:00:00+00:00 List-Id: > >I want to find the sqrt of a interger... how can I do this and what package >> >do I need to declare >[...] >> If you are using Ada95 then the package is >> >> Ada.Numerics.Generic_Elementary_Functions or >> Ada.Numerics.Elementary_Functions (for a float instanciation). > >The original question was about finding the square root of an *integer*; >Generic_Elementary_Functions and its instantiations work on floating-point >types. If you really want a function to do integer square roots, here it is (my Ada 95 syntax may not be quite right): generic 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; begin loop declare The_New_Value : constant T'Base := (The_Square_Root + N / The_Square_Root) / 2; begin exit when The_New_Value = The_Square_Root; end; end loop; return The_Square_Root; end Generic_Square_Root; This will work in Ada 83, too, by removing the references to 'Base and making sure type T has a large enough range to include the value of the square root. matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271