|
-
Dec 28th, 2002, 05:59 PM
#1
Thread Starter
Member
Exponent
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
Death is always smiling down on us, the only thing we can do is smile back
-
Dec 29th, 2002, 03:34 AM
#2
Fanatic Member
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!
-
Dec 29th, 2002, 05:14 AM
#3
Here's an integer pow function.
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;
}
I only uses integers in the calculations, so beware when using negative exponents: the rounding errors can be large.
The real pow will also probably beat this in terms of performance when the exponent rises above 4 or 5.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 29th, 2002, 07:06 AM
#4
Guru
Originally posted by CornedBee
I only uses integers in the calculations, so beware when using negative exponents: the rounding errors can be large.
Seems to me like using a negative exponent will always make your code return "0".
Btw: Fastest way (integers only!) to raise 2 to the power of x:
Code:
// x = a * 2^b
x = a << b;
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.
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);
-
Dec 29th, 2002, 07:45 AM
#5
Hyperactive Member
His code for negative exponent is wrong, he is probably rusty on his maths , but I don't see why you would get 0 either.
CornedBee :
An example:
A^-3 == 1/(A^3)
Last edited by transcendental; Dec 29th, 2002 at 07:58 AM.
-
Dec 29th, 2002, 11:13 AM
#6
Thread Starter
Member
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));
}
Death is always smiling down on us, the only thing we can do is smile back
-
Dec 29th, 2002, 02:36 PM
#7
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 29th, 2002, 03:35 PM
#8
Guru
Originally posted by CornedBee
recursion is slow.
But the main problem with recursion is its memory consumption.
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*)
Last edited by Yonatan; Dec 29th, 2002 at 03:40 PM.
-
Dec 29th, 2002, 04:29 PM
#9

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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 30th, 2002, 02:20 AM
#10
Hyperactive Member
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;
}
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.
-
Jan 2nd, 2003, 04:32 AM
#11
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|