Calculating exponents without library functions

Discussion in 'C++' started by David, Apr 11, 2004.

  1. David

    David Guest

    How can you write a program in C++ by using loop to control the calculation
    without the use of library function pwr .For example 3 to the power of 4 (ie
    3x3x3x3). Any help will be appreciated
    David, Apr 11, 2004
    #1
    1. Advertising

  2. David

    Mike Wahler Guest

    "David" <> wrote in message
    news:7Ujec.78302$Pk3.72421@pd7tw1no...
    > How can you write a program in C++ by using loop to control the

    calculation
    > without the use of library function pwr .For example 3 to the power of 4

    (ie
    > 3x3x3x3). Any help will be appreciated


    #include <iostream>

    unsigned int pwr(unsigned int base, unsigned int exp)
    {
    unsigned int result = 1;

    while(exp--)
    result *= base;

    return result;
    }

    int main()
    {
    std::cout << pwr(3, 4) << '\n';
    return 0;
    }


    -Mike
    Mike Wahler, Apr 12, 2004
    #2
    1. Advertising

  3. David

    David Guest

    Thanks a lot Mike for the solution

    I am trying to do the the calculation without the use of built in pwr
    function.I have to use the for or while loop with probably the counter

    Thanks once again



    David

    "Mike Wahler" <> wrote in message
    news:8qlec.5596$...
    > "David" <> wrote in message
    > news:7Ujec.78302$Pk3.72421@pd7tw1no...
    > > How can you write a program in C++ by using loop to control the

    > calculation
    > > without the use of library function pwr .For example 3 to the power of 4

    > (ie
    > > 3x3x3x3). Any help will be appreciated

    >
    > #include <iostream>
    >
    > unsigned int pwr(unsigned int base, unsigned int exp)
    > {
    > unsigned int result = 1;
    >
    > while(exp--)
    > result *= base;
    >
    > return result;
    > }
    >
    > int main()
    > {
    > std::cout << pwr(3, 4) << '\n';
    > return 0;
    > }
    >
    >
    > -Mike
    >
    >
    David, Apr 12, 2004
    #3
  4. David

    Mike Wahler Guest

    "David" <> wrote in message
    news:X1nec.83152$Ig.68204@pd7tw2no...
    > Thanks a lot Mike for the solution
    >
    > I am trying to do the the calculation without the use of built in pwr
    > function.


    There is no built-in function called 'pwr()' (however there
    is one called 'pow()' declared by <cmath>

    'pwr()' is the name of the function I wrote in my post.


    >I have to use the for or while loop with probably the counter


    The function I posted uses a while loop with a counter.
    (the 'counter' is the exponent parameter).

    BTW please don't top post.

    -Mike
    Mike Wahler, Apr 12, 2004
    #4
  5. David

    Matt Guest

    David wrote:
    > How can you write a program in C++ by using loop to control the calculation
    > without the use of library function pwr .For example 3 to the power of 4 (ie
    > 3x3x3x3). Any help will be appreciated


    Try posting to comp.lang.c++.homework.
    Matt, Apr 15, 2004
    #5
  6. David

    Mabden Guest

    "Matt" <> wrote in message
    news:Dspfc.1741$%...
    > David wrote:
    > > How can you write a program in C++ by using loop to control the

    calculation
    > > without the use of library function pwr .For example 3 to the power

    of 4 (ie
    > > 3x3x3x3). Any help will be appreciated

    >
    > Try posting to comp.lang.c++.homework.
    >


    I would use a loop to control the calculation. For example 3 to the
    power of 4 is the same as 3 x 3 x 3 x 3.


    Do I get part of the A?

    --
    Mabden
    Mabden, Apr 15, 2004
    #6
  7. Matt wrote:
    > David wrote:
    >
    >> How can you write a program in C++ by using loop to control the
    >> calculation
    >> without the use of library function pwr .For example 3 to the power of
    >> 4 (ie
    >> 3x3x3x3). Any help will be appreciated

    >
    >
    > Try posting to comp.lang.c++.homework.
    >


    Try something like this. It uses a redundant loop and calculates
    X to the power of Y in approximately Y! steps.


    /***************************************************************************
    main.cpp - description
    -------------------
    begin : Thu Apr 15 19:35:27 NZDT 2004
    copyright : (C) 2004 by Bruce Clement
    email :

    ***************************************************************************/

    /***************************************************************************
    *
    * This program is free software; you can redistribute it
    * and/or modify it under the terms of the GNU General Public
    * License as published by the Free Software Foundation;
    * either version 2 of the License, or (at your option) any
    * later version.
    *
    **********************************************************************/

    #include <iostream>

    double power( double x, int y )
    {
    double rslt = 1;
    for( int i = y; 0 < i--; )
    rslt = x * power( x, y - 1 );
    return rslt;
    }


    int main(int argc, char *argv[])
    {
    std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
    std::endl;

    return 0;
    }
    Bruce Clement, Apr 15, 2004
    #7
  8. In message <c5ldnc$ctu$>, Bruce Clement
    <> writes
    >Matt wrote:
    >> David wrote:
    >>
    >>> How can you write a program in C++ by using loop to control the
    >>>calculation
    >>> without the use of library function pwr .For example 3 to the power
    >>>of 4 (ie
    >>> 3x3x3x3). Any help will be appreciated

    >> Try posting to comp.lang.c++.homework.
    >>

    >
    >Try something like this. It uses a redundant loop and calculates
    >X to the power of Y in approximately Y! steps.
    >
    >
    >/***************************************************************************
    > main.cpp - description
    > -------------------
    > begin : Thu Apr 15 19:35:27 NZDT 2004
    > copyright : (C) 2004 by Bruce Clement
    > email :
    >************************************************************************
    >***/
    >
    >/***************************************************************************
    > *
    > * This program is free software; you can redistribute it
    > * and/or modify it under the terms of the GNU General Public
    > * License as published by the Free Software Foundation;
    > * either version 2 of the License, or (at your option) any
    > * later version.
    > *
    > **********************************************************************/
    >
    >#include <iostream>
    >
    >double power( double x, int y )
    >{
    > double rslt = 1;
    > for( int i = y; 0 < i--; )
    > rslt = x * power( x, y - 1 );


    Couldn't you use a recursive function here instead of operator* ?

    > return rslt;
    >}
    >
    >
    >int main(int argc, char *argv[])
    >{
    > std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
    >std::endl;
    >
    > return 0;
    >}
    >


    --
    Richard Herring
    Richard Herring, Apr 15, 2004
    #8
  9. Richard Herring wrote:
    > In message <c5ldnc$ctu$>, Bruce Clement
    > <> writes
    >
    >> Matt wrote:
    >>
    >>> David wrote:
    >>>
    >>>> How can you write a program in C++ by using loop to control the
    >>>> calculation
    >>>> without the use of library function pwr .For example 3 to the power
    >>>> of 4 (ie
    >>>> 3x3x3x3). Any help will be appreciated
    >>>
    >>> Try posting to comp.lang.c++.homework.
    >>>

    >>
    >> Try something like this. It uses a redundant loop and calculates
    >> X to the power of Y in approximately Y! steps.
    >>
    >>

    [...]
    >
    > Couldn't you use a recursive function here instead of operator* ?
    >

    [...]

    LOL, You're evil. I like that in a person. :)
    Bruce Clement, Apr 15, 2004
    #9
  10. David

    rossum Guest

    On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
    <> wrote:

    >Richard Herring wrote:
    >> In message <c5ldnc$ctu$>, Bruce Clement
    >> <> writes
    >>
    >>> Matt wrote:
    >>>
    >>>> David wrote:
    >>>>
    >>>>> How can you write a program in C++ by using loop to control the
    >>>>> calculation
    >>>>> without the use of library function pwr .For example 3 to the power
    >>>>> of 4 (ie
    >>>>> 3x3x3x3). Any help will be appreciated
    >>>>
    >>>> Try posting to comp.lang.c++.homework.
    >>>>
    >>>
    >>> Try something like this. It uses a redundant loop and calculates
    >>> X to the power of Y in approximately Y! steps.
    >>>
    >>>

    >[...]
    >>
    >> Couldn't you use a recursive function here instead of operator* ?
    >>

    >[...]
    >
    >LOL, You're evil. I like that in a person. :)



    Real programmers do it with the Peano Axioms

    rossum


    #include <iostream>
    #include <cstdlib>

    //-------------------------------------

    int successor(int num)
    {
    int result;
    result = ++num;
    return result;
    } // end successor()

    //-------------------------------------

    int add(int a, int b)
    {
    int result;
    if (b == 0)
    result = a;
    else
    result = successor(add(a, b - 1));
    return result;
    } // end add()

    //-------------------------------------

    int multiply(int a, int b)
    {
    int result;
    if (b == 0)
    result = 0;
    else
    result = add(a, multiply(a, b - 1));
    return result;
    } // end multiply()

    //-------------------------------------

    int power(int x, int y)
    {
    int result = 1;
    // An interesting variation, thanks Bruce.
    for (int i = y; 0 < i--; )
    result = multiply(x, power(x, y - 1));
    return result;
    } // end power()

    //-------------------------------------

    int main(int argc, char **argv)
    {
    std::cout << "Three raised to the power four is "
    << power(3, 4)
    << std::endl;
    return EXIT_SUCCESS;
    } // end main()




    --

    The Ultimate Truth is that there is no Ultimate Truth
    rossum, Apr 16, 2004
    #10
  11. In message <>, rossum
    <> writes
    >On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
    ><> wrote:
    >
    >>Richard Herring wrote:
    >>> In message <c5ldnc$ctu$>, Bruce Clement
    >>> <> writes
    >>>
    >>>> Matt wrote:
    >>>>
    >>>>> David wrote:
    >>>>>
    >>>>>> How can you write a program in C++ by using loop to control the
    >>>>>> calculation
    >>>>>> without the use of library function pwr .For example 3 to the power
    >>>>>> of 4 (ie
    >>>>>> 3x3x3x3). Any help will be appreciated
    >>>>>
    >>>>> Try posting to comp.lang.c++.homework.
    >>>>>
    >>>>
    >>>> Try something like this. It uses a redundant loop and calculates
    >>>> X to the power of Y in approximately Y! steps.
    >>>>
    >>>>

    >>[...]
    >>>
    >>> Couldn't you use a recursive function here instead of operator* ?
    >>>

    >>[...]
    >>
    >>LOL, You're evil. I like that in a person. :)

    >
    >
    >Real programmers do it with the Peano Axioms


    [snip program]

    Indeed. Now do it for _double_ arguments ;-)

    --
    Richard Herring
    Richard Herring, Apr 19, 2004
    #11
  12. David

    velthuijsen Guest

    > >> Couldn't you use a recursive function here instead of operator* ?
    > >>

    > >[...]
    > >
    > >LOL, You're evil. I like that in a person. :)

    >
    >
    > Real programmers do it with the Peano Axioms
    >
    > rossum


    Nice program :)
    But seeing that it takes:
    65 calls to power
    376 calls to multiply
    6036 calls to add
    5724 calls to Successor
    do you have a version with less redundancy in it while preserving the
    I'm the processor structure?
    velthuijsen, Apr 19, 2004
    #12
  13. David

    rossum Guest

    On 19 Apr 2004 04:31:23 -0700, (velthuijsen)
    wrote:

    >> >> Couldn't you use a recursive function here instead of operator* ?
    >> >>
    >> >[...]
    >> >
    >> >LOL, You're evil. I like that in a person. :)

    >>
    >>
    >> Real programmers do it with the Peano Axioms
    >>
    >> rossum

    >
    >Nice program :)
    >But seeing that it takes:
    >65 calls to power
    >376 calls to multiply
    >6036 calls to add
    >5724 calls to Successor
    >do you have a version with less redundancy in it while preserving the
    >I'm the processor structure?


    That version was designed to take recursion towards the extreme, it
    was not designed for efficiency, or for doing the OP's homework.

    If you want less redundancy then:
    1 Remove Bruce's redundant loop from power()
    2 Change all the single recursions into for loops

    Thanks for the call counts, just think yourself lucky that I didn't
    reproduce Bruce's redundant loop in all the other functions as well.

    rossum

    --

    The Ultimate Truth is that there is no Ultimate Truth
    rossum, Apr 20, 2004
    #13
  14. David

    rossum Guest

    On Mon, 19 Apr 2004 10:45:04 +0100, Richard Herring <junk@[127.0.0.1]>
    wrote:

    >In message <>, rossum
    ><> writes
    >>On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
    >><> wrote:
    >>
    >>>Richard Herring wrote:
    >>>> In message <c5ldnc$ctu$>, Bruce Clement
    >>>> <> writes
    >>>>
    >>>>> Matt wrote:
    >>>>>
    >>>>>> David wrote:
    >>>>>>
    >>>>>>> How can you write a program in C++ by using loop to control the
    >>>>>>> calculation
    >>>>>>> without the use of library function pwr .For example 3 to the power
    >>>>>>> of 4 (ie
    >>>>>>> 3x3x3x3). Any help will be appreciated
    >>>>>>
    >>>>>> Try posting to comp.lang.c++.homework.
    >>>>>>
    >>>>>
    >>>>> Try something like this. It uses a redundant loop and calculates
    >>>>> X to the power of Y in approximately Y! steps.
    >>>>>
    >>>>>
    >>>[...]
    >>>>
    >>>> Couldn't you use a recursive function here instead of operator* ?
    >>>>
    >>>[...]
    >>>
    >>>LOL, You're evil. I like that in a person. :)

    >>
    >>
    >>Real programmers do it with the Peano Axioms

    >
    >[snip program]
    >
    >Indeed. Now do it for _double_ arguments ;-)


    Only if you can give me an equivalent of the Peano Axioms (which only
    cover integers) for real numbers. The problem is how to
    mathematically define "successor" in the real number domain.

    rossum



    --

    The Ultimate Truth is that there is no Ultimate Truth
    rossum, Apr 20, 2004
    #14
  15. In message <>, rossum
    <> writes
    >On Mon, 19 Apr 2004 10:45:04 +0100, Richard Herring <junk@[127.0.0.1]>
    >wrote:
    >
    >>In message <>, rossum
    >><> writes
    >>>On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
    >>><> wrote:
    >>>
    >>>>Richard Herring wrote:
    >>>>> In message <c5ldnc$ctu$>, Bruce Clement
    >>>>> <> writes
    >>>>>
    >>>>>> Matt wrote:
    >>>>>>
    >>>>>>> David wrote:
    >>>>>>>
    >>>>>>>> How can you write a program in C++ by using loop to control the
    >>>>>>>> calculation
    >>>>>>>> without the use of library function pwr .For example 3 to the power
    >>>>>>>> of 4 (ie
    >>>>>>>> 3x3x3x3). Any help will be appreciated
    >>>>>>>
    >>>>>>> Try posting to comp.lang.c++.homework.
    >>>>>>>
    >>>>>>
    >>>>>> Try something like this. It uses a redundant loop and calculates
    >>>>>> X to the power of Y in approximately Y! steps.
    >>>>>>
    >>>>>>
    >>>>[...]
    >>>>>
    >>>>> Couldn't you use a recursive function here instead of operator* ?
    >>>>>
    >>>>[...]
    >>>>
    >>>>LOL, You're evil. I like that in a person. :)
    >>>
    >>>
    >>>Real programmers do it with the Peano Axioms

    >>
    >>[snip program]
    >>
    >>Indeed. Now do it for _double_ arguments ;-)

    >
    >Only if you can give me an equivalent of the Peano Axioms (which only
    >cover integers) for real numbers. The problem is how to
    >mathematically define "successor" in the real number domain.
    >

    <shudder> Don't fall into that trap; what "double" denotes is nothing
    like a real. It's merely a subset of the rationals.

    --
    Richard Herring
    Richard Herring, Apr 21, 2004
    #15
    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. Kwaj

    Exponents in VHDL?

    Kwaj, Aug 19, 2004, in forum: VHDL
    Replies:
    2
    Views:
    14,606
    anupam
    Aug 20, 2004
  2. Alex Vinokur
    Replies:
    2
    Views:
    2,780
    Alex Vinokur
    May 13, 2004
  3. geremy condra
    Replies:
    1
    Views:
    577
    Mark Dickinson
    Mar 8, 2010
  4. geremy condra
    Replies:
    4
    Views:
    549
    Mark Dickinson
    Mar 10, 2010
  5. geremy condra
    Replies:
    0
    Views:
    422
    geremy condra
    Mar 8, 2010
Loading...

Share This Page