x power y?

This is a discussion on x power y? within the C++ Programming forums, part of the General Programming Boards category; I made the following function to calculate x power y.. I'd like to upgrade it to make it possible to ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    x power y?

    I made the following function to calculate x power y..
    I'd like to upgrade it to make it possible to calculate even if y is decimal.. ex 2^3.456

    is that possible?
    Im not asking for full code just ideas..

    Code:
    double power(double pow, double from)	// x power y
    {
          double temp =  from;
          if(pow > 0)
          {
    	  --pow;				// x power 1 means x 1 time not 2 times !
    	  for(int i = 0; i < pow; i++)	  temp *= from;	// temp = from x times
          }
          else if(pow < 0)	  
          {
    	  --pow;					//  10/10 = 1 so must add 1..
    	  for(int i = 0; i > pow; --i)	  temp /= from;	// temp = from divided x times
          }
          else if (pow == 0) temp = 1;			// x power 0 == 1
          return temp;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    You could cheat and use pow()

    If you include math.h, you can use pow()

    double pow(double x, double y);
    float pow(float x, float y); [C++ only]
    long double pow(long double x, long double y); [C++ only]
    double pow(double x, int y); [C++ only]
    float pow(float x, int y); [C++ only]
    long double pow(long double x, int y); [C++ only]

    The function returns x raised to the power y, x^y.

  3. #3
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    that is an option but id like to try to make it myself..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    I finally decided to use math.h since I really dunno how to calculate it..
    ex: 2^2.5=5.656854249

    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    One way to calculate it yourseld is to use logarithms:

    ln( x^y ) = y * ln(x)

    x^y = e^( y*ln(x) )

    In C++:
    Code:
    double myPower(double x, double y)
    {
      return exp( y * log(x) );
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thanks Sang-drax

    I couldn't figure it out either. I was thinking that maybe logs were the answer... But, I don't know how to do logs "by hand" either. (Trial & error and interpolation?) So, you still have to use math.h. I know they (the Greeks?) were doing logs long before calculators and computers... Somebody had to figure out where to put the marks on the slide rule!

  7. #7
    ˇAmo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Yeah, I think they used a trig table and interpolation. Back then, they did out long calculations by hand. They didn't have tv and had to find something to do I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 01:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 10:56 AM

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