integral counterpart to pow()?

This is a discussion on integral counterpart to pow()? within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering, in the standard libraries, is there an integral counterpart to the "double pow(double, double)" function? (for ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145

    integral counterpart to pow()?

    Hi,
    I was wondering, in the standard libraries, is there an integral counterpart to the "double pow(double, double)" function? (for an integer raised to the power of a positive integer)

    I am asking this because I want to use the pow function without having to worry about everything associated with floating points (precision etc).

    Thank you very much.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    I was wondering, in the standard libraries, is there an integral counterpart to the "double pow(double, double)" function? (for an integer raised to the power of a positive integer)
    Unfortunately no, only versions for float, double and long double.

    I am asking this because I want to use the pow function without having to worry about everything associated with floating points (precision etc).
    Thankfully, it is much easier to write a version for an int raised to an int, so you could always write it yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    thank you for your reply.

    Thankfully, it is much easier to write a version for an int raised to an int, so you could always write it yourself.
    Can you please enlighten me a bit on that? It doesn't sound too easy to me. Unless the most efficient way is to just loop through the exponent?

    Thank you very much

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Unless the most efficient way is to just loop through the exponent?
    An efficient yet easy way (not sure if it is the most efficient) is to note that:
    For a integers x, n and letting ^ denote exponentiation:
    x^(2n) = x^n * x^n
    x^(2n+1) = x^n * x^n * x

    Consequently, one could write a recursive function that performs this calculation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    An efficient yet easy way (not sure if it is the most efficient) is to note that:
    For a integers x, n and letting ^ denote exponentiation:
    x^(2n) = x^n * x^n
    x^(2n+1) = x^n * x^n * x
    hmm I see... I will try to implement that.

    Thank you for your help

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    It doesn't need to be recursive:
    Code:
    	unsigned int pow(unsigned int a, unsigned int b) {
    		unsigned int result = 1;
    		while (b) {
    			if (b & 1)
    				result *= a;
    			b >>= 1;
    			a *= a;
    		}
    		return result;
    	}
    Last edited by iMalc; 11-23-2007 at 09:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the difference between pow() vs shift?
    By Overworked_PhD in forum C Programming
    Replies: 9
    Last Post: 01-06-2008, 12:04 PM
  2. Help with pow()
    By jedi_jinn in forum C Programming
    Replies: 7
    Last Post: 10-09-2006, 02:17 AM
  3. Problem With Pow Function, Help?
    By verd in forum C++ Programming
    Replies: 11
    Last Post: 02-27-2006, 09:56 AM
  4. trying to use pow()
    By Mikecore in forum C Programming
    Replies: 9
    Last Post: 10-09-2005, 06:30 PM
  5. Problem with pow()
    By RMacFly in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21