Thread: How do I do this without the pow() function?

  1. #1
    Unregistered
    Guest

    Unhappy How do I do this without the pow() function?

    First off let say thank you for even taking the time to read this post.

    What I want to do is write a program that evalutes the powers given the base and the exponent without using the pow() function.

    What I decided to do have the base multiplied to it's self x amount of times, x being exponent. It seems easy to do but I can't seem to get it right. Any help appreciated!

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    have a function take the appropriate parameters, set the return value equal to the base, and loop multiply it the number of exponent times...
    hasafraggin shizigishin oppashigger...

  3. #3
    Unregistered
    Guest

    Thumbs up

    I figured it out right before you posted. Thanks anyway!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    If you do like float^integer then you can use my method for faster code (between 32 and 64 multiplies for a 32-bit exponent). Here it is:

    Code:
    float powfl(float b, unsigned long x)
    {
     unsigned long bit = 1;
     float total = 1.f;
     float mult = b;
     for(long c = 0; c < 32; c++)
     {
      if(x & bit)
      {
       total *= mult;
      }
      mult *= mult;
      bit <<= 1;
     }
     return total;
    }
    Did I do it right?
    Last edited by gliptic; 12-12-2001 at 01:04 AM.
    // Gliptic

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi
    if the power factor is not an integer you may use exponents and logarithms to do powers

    1. a = y^x
    2. log(a) = log(y^x)
    3. log(a) = x*log(y)
    4. exp(log(a)) = exp(x*log(y))

    or finaly:

    5. a = exp(x*log(y))

    where log is natural logarithm (with base 'e')

    damyan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM