comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Unsigned Integer Restraint Errors
Date: 13 Mar 2007 09:04:14 -0700
Date: 2007-03-13T09:04:14-07:00	[thread overview]
Message-ID: <1173801854.505211.245650@64g2000cwx.googlegroups.com> (raw)
In-Reply-To: <1173797935.875023.7590@p10g2000cwp.googlegroups.com>

On Mar 13, 7:58 am, "frikk" <frik...@gmail.com> wrote:
> I have a bit of an unrelated (but kind of related) question:
>
> Are there any builtin square root functions? I thought this was
> working, but I don't think it is anymore:
> m := Integer(100**(0.5)+1);

This couldn't have worked as written, since there is no predefined
"**" that accepts a floating-point number on the right side.

> This always coming out to 2. I;m using this to computer a maximum loop
> value for finding primes. I'd like to loop from 2 to sqrt(n)/2 + 1.
>
> Any suggestions?

See Section A.5.1.  Ada.Numerics.Elementary_Functions (A.5.1(9))
defines Sqrt as well as a "**" that accepts floating-point exponents.

By the way, if I were going to write a loop that goes from 2 to
sqrt(n)/2 + 1, I wouldn't use Sqrt like this:

    for I in 2 .. Integer(Float'Floor(Sqrt(Float(N))/2.0)) + 1 loop...

but rather, I'd just write it something like this:

    I := 2;
    while (2 * I - 1) * (2 * I - 1) <= N loop -- I think I got this
right
        ...
        I := I + 1;
    end loop;

on the theory that using Sqrt is just a waste of cycles when you can
just use integer arithmetic.  But I've been programming for way too
long---long enough that I remember what a Hollerith card is---and I'm
sure that doing a square-root doesn't take nearly as long as it used
to, so there are probably some reasonable values of N for which
computing the square-root first makes things more efficient than the
repeated integer multiplications.  I don't know.  Use Sqrt if you want
to.

                                 -- Adam





  parent reply	other threads:[~2007-03-13 16:04 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-12 15:07 Unsigned Integer Restraint Errors frikk
2007-03-12 16:27 ` Georg Bauhaus
2007-03-12 17:17 ` Adam Beneschan
2007-03-12 17:23 ` Adam Beneschan
2007-03-12 18:11   ` frikk
2007-03-12 20:00     ` frikk
2007-03-12 20:07       ` Adam Beneschan
2007-03-12 18:00 ` Dmitry A. Kazakov
2007-03-12 19:00   ` Martin Krischik
2007-03-12 21:13     ` Dmitry A. Kazakov
2007-03-12 19:13   ` frikk
2007-03-12 19:22     ` Randy Brukardt
2007-03-13  3:13       ` Jeffrey R. Carter
2007-03-13  3:00         ` Randy Brukardt
2007-03-13 12:09           ` frikk
2007-03-13 14:58             ` frikk
2007-03-13 15:31               ` frikk
2007-03-13 15:59                 ` Robert A Duff
2007-03-13 16:18                 ` Dmitry A. Kazakov
2007-03-13 16:21                 ` Jeffrey R. Carter
2007-03-13 16:04               ` Adam Beneschan [this message]
2007-03-13 16:41                 ` Adam Beneschan
2007-03-13 16:42                   ` Adam Beneschan
2007-03-14 14:06                     ` frikk
2007-03-13 17:23                 ` Dmitry A. Kazakov
2007-03-13 17:31                   ` Adam Beneschan
2007-03-14  0:54                   ` Jeffrey R. Carter
2007-03-16 13:38                   ` frikk
2007-03-13 16:16           ` Jeffrey R. Carter
2007-03-12 21:04     ` Dmitry A. Kazakov
replies disabled

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