Raising a number to an exponent without writing a function.

Started by
7 comments, last by Roof Top Pew Wee 21 years, 3 months ago
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--
Advertisement
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.
the functions name is pow()


Runicsoft -- latest attraction: obfuscated Brainfuck Interpreter in SML

This post was made entirely from re-cycled electrons
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?
Oops, that one above is me.

--Vic--

[edited by - Roof Top Pew Wee on January 18, 2003 4:55:02 PM]
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]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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.
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.
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

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement