Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Approximate integer square roots

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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"
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a pricing function (Extracting Square Roots) Falk Köppe Java 6 12-16-2009 02:44 PM
integer square roots timro21 Python 13 07-24-2009 05:36 AM
Compilation Error using ANN Approximate nearest neighbour library Travis Stewart C++ 4 06-09-2004 06:25 PM
SRT DIvision, Square root and reciprocal square root alghazo@siu.edu VHDL 0 05-27-2004 06:23 AM
Can you approximate subranges in C? Paul Eggert C Programming 9 07-07-2003 07:06 AM



Advertisments