Results 1 to 11 of 11

Thread: Exponent

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34

    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

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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!

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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);

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594


    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.

  10. #10
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width