Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calculating exponents without library functions

Reply
Thread Tools

Calculating exponents without library functions

 
 
David
Guest
Posts: n/a
 
      04-11-2004
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




 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      04-12-2004
"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


 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      04-12-2004
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$ ink.net...
> "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
>
>



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      04-12-2004
"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



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      04-15-2004
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.

 
Reply With Quote
 
Mabden
Guest
Posts: n/a
 
      04-15-2004
"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


 
Reply With Quote
 
Bruce Clement
Guest
Posts: n/a
 
      04-15-2004
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 : kiore-at-kiore-dot-

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

/************************************************** *************************
*
* 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;
}

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      04-15-2004
In message <c5ldnc$ctu$>, Bruce Clement
<kiore-at-kiore-dot-> 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 : kiore-at-kiore-dot-
>************************************************* ***********************
>***/
>
>/************************************************** *************************
> *
> * 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
 
Reply With Quote
 
Bruce Clement
Guest
Posts: n/a
 
      04-15-2004
Richard Herring wrote:
> In message <c5ldnc$ctu$>, Bruce Clement
> <kiore-at-kiore-dot-> 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.

 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      04-16-2004
On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
<kiore-at-kiore-dot-> wrote:

>Richard Herring wrote:
>> In message <c5ldnc$ctu$>, Bruce Clement
>> <kiore-at-kiore-dot-> 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
 
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
Re: Calculating very large exponents in python geremy condra Python 4 03-10-2010 05:51 PM
Re: Calculating very large exponents in python geremy condra Python 1 03-08-2010 07:21 PM
Re: Calculating very large exponents in python geremy condra Python 0 03-08-2010 07:08 PM
Exponents in VHDL? Kwaj VHDL 2 08-20-2004 04:52 AM
STL & Generating exponents, permutations, arrangements, and combinations Alex Vinokur C++ 2 05-13-2004 02:14 PM



Advertisments