# Calculating exponents without library functions

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

1. ### DavidGuest

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

2. ### Mike WahlerGuest

"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

3. ### DavidGuest

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
4. ### Mike WahlerGuest

"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).

-Mike

Mike Wahler, Apr 12, 2004
5. ### MattGuest

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
6. ### MabdenGuest

"Matt" <> wrote in message
newsspfc.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
7. ### Bruce ClementGuest

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
* 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
8. ### Richard HerringGuest

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
> * 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
9. ### Bruce ClementGuest

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
10. ### rossumGuest

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 result;
if (b == 0)
result = a;
else
result = successor(add(a, b - 1));
return result;

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

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
11. ### Richard HerringGuest

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
12. ### velthuijsenGuest

> >> 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
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
13. ### rossumGuest

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
>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
14. ### rossumGuest

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
15. ### Richard HerringGuest

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