#### Archived

This topic is now archived and is closed to further replies.

# Raising a number to an exponent without writing a function.

This topic is 5729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

From the help files: // Example of the bitwise-exclusive-OR operator int nNumA=9, nNumB=3, nNumC; // 00001001, 00000011 nNumC = nNumA ^ nNumB; // nNumC is now 10: 00001010 So if I wanted to get 9^3, I could write a function that would just create a loop and multiply the number over and over, but I thought maybe there was some faster way to do this in C++. Is there a function already written to raise numbers to an exponent? If not, then I can just write a quick one myself. It''s not an issue of lazines, but rather of speed. I''ve searched the sdk and haven''t been able to find anything else on the subject. Thanks for any help, --Vic--

##### Share on other sites
If the exponent is a constant you could use template meta-programming to create an automatically expanding function, that would create the function for you.

##### Share on other sites
the functions name is pow()

Runicsoft -- latest attraction: obfuscated Brainfuck Interpreter in SML

This post was made entirely from re-cycled electrons

##### Share on other sites
Ummm, I''m not worried about creating the function, if that''s what you''re telling me. I could just create a simple function like:

float exponent(float numToMultiply, int exponent)
{
float floatToReturn = numToMultiply;
for(int i = 0; i <= exponent; i++)
floatToReturn*= numToMultiply;

if(exponent != 0)
return floatToReturn;
else
return 1;
}

Perhaps I didn''t understand what you were saying. I''m not sure what a template meta-programming is; nor do I know what an automatically expanding function is.

Any other help?

--Vic--

P.S. If I were to write my own function, how would I do decimal exponents?

##### Share on other sites
Oops, that one above is me.

--Vic--

[edited by - Roof Top Pew Wee on January 18, 2003 4:55:02 PM]

##### Share on other sites
pow(x, y); its in math.h
returns x^y (mathematically)

I think it's overloaded for different number types.

P.S. It works with decimal exponents too. It'd be slightly difficult to do it on your own but you could get a good start looking on the web. AHH, why on Earth would you want to use template metaprograming?!

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

[edited by - Thunder_Hawk on January 18, 2003 4:56:28 PM]

##### Share on other sites
Here is an example of a template metaprogram to raise a integer to an integer power:

  template<int M, int N>class Power {public:enum { value = M * Power<M,N-1>::value};};class Power<0, int N> {public:enum { value = 1};};class Power<M, 1> {public:enum { value = M};};

And then you can write expressions such as:

int i = Power<3, 2>::value;

Why use metaprogamming? The compiler can optimise away some of the code. In the case of the parameters being constant the compiler can optimise the above expression to:

int i = 9;

Which avoids the multiplications at runtime.

##### Share on other sites
I think that in order to do floating point powers you would need to use the binomial expansion:

(1 + x) ^ n = 1 + nx + n(n-1)/2! *x^2 + n(n-1)(n-2)/3! * x ^3 + ...

This is an infinite series if you can''t see the pattern just ask.
You just need to use enough terms to get the degree of accuracy required.

##### Share on other sites
You could also represent the number as an IEEE754 float and fiddle with the exponent used before casting back - about 3 instructions ;o)

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×