Velocity Reviews > Approximate integer square roots

# Approximate integer square roots

Albert
Guest
Posts: n/a

 01-30-2009
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

Fred
Guest
Posts: n/a

 01-30-2009
On Jan 30, 2:58*pm, Albert <(E-Mail Removed)> 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?
>

No

--
Fred K

Nelu
Guest
Posts: n/a

 01-30-2009
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

Keith Thompson
Guest
Posts: n/a

 01-30-2009
Albert <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"

Eric Sosman
Guest
Posts: n/a

 01-30-2009
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
(E-Mail Removed)lid