Archived

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

Roof Top Pew Wee

Raising a number to an exponent without writing a function.

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites