Is there an exponent sign in C++, when i tried putting in ^ , it gave me an error. Or do u have to have a for loop to do exponents?
Thanks:D :D
Printable View
Is there an exponent sign in C++, when i tried putting in ^ , it gave me an error. Or do u have to have a for loop to do exponents?
Thanks:D :D
Include <math.h>
There's a power function: pow(base, exponent).
A tip: when you need to raise something to the power of two (or three), then your program will be much faster when you just multiply it, instead of using the pow function: base * base (* base)
This is especially true for integer bases, since pow converts everything to double
Good luck!
Here's an integer pow function.
I only uses integers in the calculations, so beware when using negative exponents: the rounding errors can be large.Code:inline int ipow(int a, int b)
{
int i, out=a;
if(b > 0)
for(i=1;i<b;++i)
out*=a;
else
for(i=-1;i<-b;++i)
out/=a;
return out;
}
The real pow will also probably beat this in terms of performance when the exponent rises above 4 or 5.
Seems to me like using a negative exponent will always make your code return "0".Quote:
Originally posted by CornedBee
I only uses integers in the calculations, so beware when using negative exponents: the rounding errors can be large.
Btw: Fastest way (integers only!) to raise 2 to the power of x:
This is called a left-shift, it actually shifts the number a, b bits to the left, adding zeroes on the right. The result is similar to multiplication by 2^b.Code:// x = a * 2^b
x = a << b;
C++ note: In C++, the operator << is overloaded by output streams. Be careful when using an actual shift. For example, these two lines are very different:Code:cout << a << b;
cout << (a << b);
His code for negative exponent is wrong, he is probably rusty on his maths:p, but I don't see why you would get 0 either.
CornedBee :
An example:
A^-3 == 1/(A^3)
Thanks, how about if u use recursion like this
Code:unsigned long GetPower(n, power)
{
if (power == 1)
return n;
else
return (n * GetPower(n,power - 1));
}
My code is absolutly correct in theory. For all practical purposes however it won't work because a^b with any b < 0 is betweeen -1 and 1 and therefore rounded to 0 or -1 using integer precision.
But don't be so quick with saying my code is wrong, because my code isn't wrong as a rule.
And I'm NOT rusty on my maths.
Chimp: recursion is slow.
But the main problem with recursion is its memory consumption.Quote:
Originally posted by CornedBee
recursion is slow.
Avoid recursion whenever possible. With a simple mathematical task such as exponent, an iterative algorithm is very much preferred. A recursive exponent function is something they teach you in introductory programming courses to teach recursion - nobody uses it for practical purposes.
The problem with CornedBee's code is not the algorithm, but the data type. If int was changed to float/double, it'd work. However, if float/double was used, a better solution than an iterative algorithm is to utilize the FPU (Floating Point Unit, part of the CPU), which is what the pow() function is supposed to do.
(Ugh, I've edited this post 4 times. *stops*) :)
:)
As I said.
I actually didn't think of it when I wrote the function. Only later I realized that it isn't useful, but I was too lazy to change it, as I don't use it anway.
Looks like I was too quick to condemn CornedBee. Yep, his algorithm is correct.It was just that I didn't take note that -b had inverted the negative sign.Code:inline int ipow(int a, int b)
{
int i, out=a;
if(b > 0)
for(i=1;i<b;++i)
out*=a;
else
for(i=-1;i<-b;++i)
out/=a;
return out;
}
here's an algoritm for integer powers that operates at O(log2(n)), i guess it could be extended for negative powers but that wasn't on my mind that time.
http://www.vbforums.com/showthread.p...wer#post531771