# Approximate integer square roots

Discussion in 'C Programming' started by Albert, Jan 30, 2009.

1. ### AlbertGuest

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

Albert, Jan 30, 2009

2. ### FredGuest

On Jan 30, 2:58 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?
>

No

--
Fred K

Fred, Jan 30, 2009

3. ### NeluGuest

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

Nelu, Jan 30, 2009
4. ### Keith ThompsonGuest

Albert <> 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.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 30, 2009
5. ### Eric SosmanGuest

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
lid

Eric Sosman, Jan 30, 2009