comp.lang.ada
 help / color / mirror / Atom feed
From: kst@thomsoft.com (Keith Thompson)
Subject: Re: Ada function sqrt(x)
Date: 1996/09/20
Date: 1996-09-20T00:00:00+00:00	[thread overview]
Message-ID: <Dy0xBw.A1u@thomsoft.com> (raw)
In-Reply-To: 51o7nh$5vl@cf01


In <51o7nh$5vl@cf01> pascal.obry@der.edfgdf.fr (Pascal Obry) writes:
> "zeppelin" <zeppelin@access.mountain.net> wrote:
> 
> >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.

You can convert the argument to a floating-point type, take the square
root, and convert back to an integer type, but that may lose precision
in some cases.  Also, I think an integer square root operation typically
truncates rather than rounding (for example, sqrt(99) is 9, not 10).
Ada 95 provides a 'Truncation attribute (see RM95-A.5.3) -- but again,
watch out for loss of precision (sqrt(9.0) *might* return something like
2.9999999; also, many 32-bit integers cannot be represented exactly in
32-bit floating-point).

You might have better luck writing an integer sqrt function that only
uses integer arithmetic.  I'm sure this has been done before; perhaps
someone else knows where.

-- 
Keith Thompson (The_Other_Keith) kst@thomsoft.com <*>
TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2706
FIJAGDWOL




  reply	other threads:[~1996-09-20  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <01bba50c$7c1a2760$dc014dc6@MountainNet>
1996-09-18  0:00 ` Ada function sqrt(x) Pascal Obry
1996-09-20  0:00   ` Keith Thompson [this message]
1996-09-20  0:00     ` Roderick Chapman
1996-09-21  0:00     ` Robert Dewar
1996-09-21  0:00     ` Robert Dewar
1996-09-26  0:00       ` Keith Thompson
1996-09-22  0:00     ` Matthew Heaney
1996-09-22  0:00       ` Matthew Heaney
1996-09-24  0:00 W. Wesley Groleau (Wes)
replies disabled

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