Approximate integer square roots

A

Albert

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
 
F

Fred

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
 
N

Nelu

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.
 
K

Keith Thompson

Albert said:
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?
 
E

Eric Sosman

Albert said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top