Trouble calculating 5th root of a given number using C

Discussion in 'C Programming' started by Kishor, Feb 2, 2004.

  1. Kishor

    Kishor Guest

    Hi Friends

    Please help me to write a C program to find the 5th (fifth) root of a given number.
    Ex:(1) Input : 32
    Output : 5th root of 32 is 2

    Ex:(1) Input : 243
    Output : 5th root of 243 is 3

    Click here : www.c4swimmers.esmartguy.com to Test Your C Programming Strengths.


    Waiting for your reply.

    Regards
    Kishor
    Kishor, Feb 2, 2004
    #1
    1. Advertising

  2. Kishor

    Allan Bruce Guest

    "Kishor" <> wrote in message
    news:...
    > Hi Friends
    >
    > Please help me to write a C program to find the 5th (fifth) root of a

    given number.
    > Ex:(1) Input : 32
    > Output : 5th root of 32 is 2
    >
    > Ex:(1) Input : 243
    > Output : 5th root of 243 is 3
    >
    > Click here : www.c4swimmers.esmartguy.com to Test Your C Programming

    Strengths.
    >
    >
    > Waiting for your reply.
    >
    > Regards
    > Kishor


    Is one allowed to use the standard math.h ?
    If so, then
    double result = pow(32, 1/5);
    should give you 2.0
    Allan
    Allan Bruce, Feb 2, 2004
    #2
    1. Advertising

  3. Kishor

    nrk Guest

    Allan Bruce wrote:

    >
    > "Kishor" <> wrote in message
    > news:...
    >> Hi Friends
    >>
    >> Please help me to write a C program to find the 5th (fifth) root of a

    > given number.
    >> Ex:(1) Input : 32
    >> Output : 5th root of 32 is 2
    >>
    >> Ex:(1) Input : 243
    >> Output : 5th root of 243 is 3
    >>
    >> Click here : www.c4swimmers.esmartguy.com to Test Your C Programming

    > Strengths.
    >>


    I ran some of the code there through the DS9K, and now there are nasal
    demons all over the place :)

    >>
    >> Waiting for your reply.
    >>
    >> Regards
    >> Kishor

    >
    > Is one allowed to use the standard math.h ?
    > If so, then
    > double result = pow(32, 1/5);
    > should give you 2.0


    Fortunately, it gives me 1.0 as expected :) Perhaps you meant this?:

    double result = pow(32, 1/5.);

    -nrk.

    > Allan


    --
    Remove devnull for email
    nrk, Feb 2, 2004
    #3
  4. Kishor

    P.J. Plauger Guest

    "Allan Bruce" <> wrote in message
    news:bvltvf$iks$2surf.net...

    > "Kishor" <> wrote in message
    > news:...
    > > Hi Friends
    > >
    > > Please help me to write a C program to find the 5th (fifth) root of a

    > given number.
    > > Ex:(1) Input : 32
    > > Output : 5th root of 32 is 2
    > >
    > > Ex:(1) Input : 243
    > > Output : 5th root of 243 is 3
    > >
    > > Click here : www.c4swimmers.esmartguy.com to Test Your C Programming

    > Strengths.
    > >
    > >
    > > Waiting for your reply.
    > >
    > > Regards
    > > Kishor

    >
    > Is one allowed to use the standard math.h ?
    > If so, then
    > double result = pow(32, 1/5);
    > should give you 2.0


    Or 1.0 at the very least. Try 1.0/5.0.

    This approach is good enough for a homework assignment,
    but not industrial strength.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Feb 2, 2004
    #4
  5. Kishor

    osmium Guest

    P.J. Plauger writes:

    > Or 1.0 at the very least. Try 1.0/5.0.
    >
    > This approach is good enough for a homework assignment,
    > but not industrial strength.


    I take it for granted that you are right. But what would you recommend for
    someone who does not wish to make this a life's work?
    osmium, Feb 2, 2004
    #5
  6. Kishor wrote:

    > Hi Friends
    >
    > Please help me to write a C program to find the 5th (fifth) root of a given number.
    > Ex:(1) Input : 32
    > Output : 5th root of 32 is 2
    >
    > Ex:(1) Input : 243
    > Output : 5th root of 243 is 3


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <float.h>

    inline double root(double x)
    {
    return pow(x, 0.2);
    }
    inline double original(double x)
    {
    return pow(x, 5.);
    }

    int main(void)
    {
    size_t pass;
    const size_t maxpass = 10;
    double x;
    const double upperlimit = 1000.;
    srand((unsigned) time(0));
    for (pass = 0; pass < maxpass; pass++) {
    x = upperlimit * rand() / (1. + RAND_MAX);
    printf("number: %.*g\n", DBL_DIG, x);
    printf("5th root: %.*g\n", DBL_DIG, x = root(x));
    printf("root to 5th power: %.*g\n\n", DBL_DIG, original(x));
    }
    return 0;
    }



    number: 349.126742687076
    5th root: 3.22549685893672
    root to 5th power: 349.126742687076

    number: 933.513863012195
    5th root: 3.92666773917324
    root to 5th power: 933.513863012195

    number: 337.094322778285
    5th root: 3.20295094809955
    root to 5th power: 337.094322778285

    number: 921.04862164706
    5th root: 3.91612469036527
    root to 5th power: 921.04862164706

    number: 193.956033326685
    5th root: 2.86774584184895
    root to 5th power: 193.956033326685

    number: 536.526418756694
    5th root: 3.51494247913713
    root to 5th power: 536.526418756694

    number: 769.721171818674
    5th root: 3.77804117237769
    root to 5th power: 769.721171818674

    number: 245.490406174213
    5th root: 3.00612409691297
    root to 5th power: 245.490406174213

    number: 818.889678921551
    5th root: 3.82512013459173
    root to 5th power: 818.889678921551

    number: 422.267477028072
    5th root: 3.35056098160768
    root to 5th power: 422.267477028072



    --
    Martin Ambuhl
    Martin Ambuhl, Feb 2, 2004
    #6
  7. Allan Bruce wrote:


    > Is one allowed to use the standard math.h ?
    > If so, then
    > double result = pow(32, 1/5);
    > should give you 2.0


    Bullshit. It yields 1.0



    --
    Martin Ambuhl
    Martin Ambuhl, Feb 2, 2004
    #7
  8. Kishor

    Lew Pitcher Guest

    On Mon, 02 Feb 2004 18:45:09 GMT, Martin Ambuhl <> wrote:

    >Allan Bruce wrote:
    >
    >
    >> Is one allowed to use the standard math.h ?
    >> If so, then
    >> double result = pow(32, 1/5);
    >> should give you 2.0

    >
    >Bullshit. It yields 1.0


    Forgive me if I misunderstand, but could someone explain to me what value 32^0
    is? If I read the statement
    >> double result = pow(32, 1/5);

    correctly, that's what the statement is computing.

    --
    Lew Pitcher
    IT Consultant, Enterprise Technology Solutions
    Toronto Dominion Bank Financial Group

    (Opinions expressed are my own, not my employers')
    Lew Pitcher, Feb 2, 2004
    #8
  9. Lew Pitcher <> spoke thus:

    > Forgive me if I misunderstand, but could someone explain to me what value 32^0
    > is? If I read the statement
    >>> double result = pow(32, 1/5);

    > correctly, that's what the statement is computing.


    You're reading it correctly. 32 (or any number other than 0) raised
    to the zero power gives 1.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 2, 2004
    #9
  10. Kishor

    Allan Bruce Guest

    "Martin Ambuhl" <> wrote in message
    news:VUwTb.7427$...
    > Allan Bruce wrote:
    >
    >
    > > Is one allowed to use the standard math.h ?
    > > If so, then
    > > double result = pow(32, 1/5);
    > > should give you 2.0

    >
    > Bullshit. It yields 1.0
    >
    >
    >


    sorry it should have read
    double result = pow(32.0, 1/5.0);
    Allan Bruce, Feb 2, 2004
    #10
  11. Kishor

    Grumble Guest

    Christopher Benson-Manica wrote:

    > 32 (or any number other than 0) raised to the zero power gives 1.


    http://mathworld.wolfram.com/ExponentLaws.html

    The definition 0^0=1 is sometimes used to simplify
    formulas, but it should be kept in mind that this
    equality is a definition and not a fundamental
    mathematical truth (Knuth 1992; Knuth 1997, p. 56).

    <g>
    Grumble, Feb 3, 2004
    #11
  12. Kishor

    Sidney Cadot Guest

    Grumble wrote:

    > Christopher Benson-Manica wrote:
    >
    >> 32 (or any number other than 0) raised to the zero power gives 1.

    >
    >
    > http://mathworld.wolfram.com/ExponentLaws.html
    >
    > The definition 0^0=1 is sometimes used to simplify
    > formulas, but it should be kept in mind that this
    > equality is a definition and not a fundamental
    > mathematical truth


    That's quite a weird statement, if you think about it. I know it's not
    your statement but Mathworld's, but still.

    To my surprise (and to get on-topic), C99 doesn't really say what
    pow(0.0, 0.0) should be:

    "The pow functions compute x raised to the power y. A domain error
    occurs if x is finite and negative and y is finite and not an integer
    value. A domain error may occur if x is zero and y is less than or equal
    to zero. A range error may occur."

    > (Knuth 1992; Knuth 1997, p. 56).


    Knuth Vol 1, third edition, seems to be giving more info on binomials
    than I could ever care to know on page 56, but nothing about exponentials.

    Best regards,

    Sidney
    Sidney Cadot, Feb 3, 2004
    #12
  13. Kishor

    Guest

    Sidney Cadot <> wrote:
    >
    > To my surprise (and to get on-topic), C99 doesn't really say what
    > pow(0.0, 0.0) should be:


    It does in Annex F (for IEEE floating-point).

    -Larry Jones

    Summer vacation started! I can't be sick! -- Calvin
    , Feb 3, 2004
    #13
  14. Kishor

    Sidney Cadot Guest

    wrote:

    > Sidney Cadot <> wrote:
    >
    >>To my surprise (and to get on-topic), C99 doesn't really say what
    >>pow(0.0, 0.0) should be:

    >
    >
    > It does in Annex F (for IEEE floating-point).


    So I see! F.9.4.4:

    "pow(x,±0) returns 1 for any x, even a NaN."

    The NaN clause is a surprise, to me at least.

    Thanks and regards,

    Sidney
    Sidney Cadot, Feb 3, 2004
    #14
    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. Asad
    Replies:
    2
    Views:
    444
    Matt Berther
    Apr 27, 2004
  2. James
    Replies:
    5
    Views:
    457
    Stefan Ram
    Sep 29, 2005
  3. Frank Buss
    Replies:
    3
    Views:
    4,068
    Mike Treseler
    Jul 31, 2006
  4. Protoman
    Replies:
    14
    Views:
    1,764
    Alf P. Steinbach
    Sep 17, 2005
  5. Erik the Red
    Replies:
    4
    Views:
    173
    Chris Pine
    Jul 29, 2005
Loading...

Share This Page