Approximate integer square roots

  • Follow


Hi everyone!

If I write in pseudocode
include math.h
/*... */
approx. sqrt = sqrt((double) x) + 1

for any unsigned int x, will the approx sqrt of 4 be 3, the approx
sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
be 8?

Thanks
0
Reply albert.xtheunknown0 (176) 1/30/2009 10:58:44 PM

On Jan 30, 2:58=A0pm, Albert <albert.xtheunkno...@gmail.com> wrote:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt =3D sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
>
 No

--
Fred K
0
Reply fred.l.kleinschmidt (236) 1/30/2009 11:04:43 PM


On Fri, 30 Jan 2009 14:58:44 -0800, Albert wrote:

> Hi everyone!
> 
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
> 
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx sqrt
> of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49 be 8?

You could write the C code and find out for yourself. 

What do you mean by approx? From your expression you can have many 
approximations: 8, 7.9, 7.93 etc.






-- 
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org


0
Reply spamahead (251) 1/30/2009 11:14:00 PM

Albert <albert.xtheunknown0@gmail.com> writes:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1

It would be helpful if you showed us some real code.  I guess
"approx. sqrt" is supposed to be an approximate square root, but what
is its type?

> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?

What are you trying to do, and why are you adding 1 to the result?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21545) 1/30/2009 11:15:57 PM

Albert wrote:
> Hi everyone!
> 
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
> 
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?

     Do you intend to convert the root-plus-one to integer?
If so, three-quarters of your predictions are correct[*] but
the "approx sqrt" of 48 will be 7, not 8.

     [*] Probably.  There are few guarantees on the accuracy
of the sqrt() function, and it is conceivable -- unlikely, but
conceivable -- that sqrt(49.0) might yield a number close to
but not exactly equal to 7.0.  If it's a tiny bit small, like
6.999999999999998446, you'll get 7, not 8, as the "approx sqrt"
of 49.

     What problem are you trying to solve?

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
Reply esosman2 (2945) 1/30/2009 11:20:22 PM

"Albert" <albert.xtheunknown0@gmail.com> wrote in message news:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
>
  sqrt(x), no need for a cast, will give to the square root of x as a 
double, within the limits of the sqrt() fucntion accuracy.
To reduce to an integer, the best way is

(int) floor( sqrt(x) + 0.5 );

if you want to always round up for some reason, use

(int) ceil( sqrt(x) );

You can bypass the floor() and ceil() routines with careful casting, but it 
is better to get into the habit of doing it properly.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
Reply regniztar (3128) 1/31/2009 5:54:37 PM

Albert wrote:
> Hi everyone!
> 
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
> 
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?

When (a) is the largest integer that is
less than or equal to the square root of (n), then:

     (n > a * a + a)

is the expression to use to determine whether to round
(a) up or down to get an approximation of the square root.

/* BEGIN new.c output */

  n  sqrt(n)   ap_sq

  0  0.000000  0
  1  1.000000  1
  2  1.414214  1
  3  1.732051  2
  4  2.000000  2
  5  2.236068  2
  6  2.449490  2
  7  2.645751  3
  8  2.828427  3
  9  3.000000  3
10  3.162278  3
11  3.316625  3
12  3.464102  3
13  3.605551  4
14  3.741657  4
15  3.872983  4
16  4.000000  4
17  4.123106  4
18  4.242641  4
19  4.358899  4
20  4.472136  4
21  4.582576  5
22  4.690416  5
23  4.795832  5
24  4.898979  5
25  5.000000  5

/* END new.c output */

/* BEGIN new.c */

#include <stdio.h>
#include <math.h>

#define LIMIT       26

unsigned ap_sq(unsigned n);

int main(void)
{
     unsigned n;

     puts("/* BEGIN new.c output */\n");
     puts(" n  sqrt(n)   ap_sq\n");
     for (n = 0; n != LIMIT; ++n) {
         printf("%2u  %f %2u\n", n, sqrt(n), ap_sq(n));
     }
     puts("\n/* END new.c output */");
     return 0;
}

unsigned ap_sq(unsigned n)
{
     unsigned a, b;

     a = n;
     for (b = ((1 == a) + a) / 2; a > b; b = (n / a + a) / 2) {
         a = b;
     }
     return (n > a * a + a) + a;
}

/* END new.c */





-- 
pete
0
Reply pfiland (6614) 1/31/2009 8:12:40 PM

6 Replies
42 Views

(page loaded in 0.092 seconds)

Similiar Articles:













7/27/2012 11:32:49 PM


Reply: