Approximate integer square roots

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

  1. Albert

    Albert Guest

    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
    #1
    1. Advertising

  2. Albert

    Fred Guest

    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
    #2
    1. Advertising

  3. Albert

    Nelu Guest

    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
    #3
  4. 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
    #4
  5. Albert

    Eric Sosman Guest

    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
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,201
  2. Travis Stewart
    Replies:
    4
    Views:
    620
    Joes Staal
    Jun 9, 2004
  3. Paul Eggert

    Can you approximate subranges in C?

    Paul Eggert, Jul 4, 2003, in forum: C Programming
    Replies:
    9
    Views:
    404
    Clive D. W. Feather
    Jul 7, 2003
  4. timro21

    integer square roots

    timro21, Jul 24, 2009, in forum: Python
    Replies:
    13
    Views:
    900
    timro21
    Jul 24, 2009
  5. Falk Köppe
    Replies:
    6
    Views:
    941
Loading...

Share This Page