Thread: Power

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Power

    I have this code to find the power of x^y. i already found out how to do x^y but am having trouble doing x^-y (x to the -y power) Here is my code
    Code:
    #include <iostream.h>
    int main()
    {
       double x,y;
       double sum = 1;
    
      cout << "Enter the X number : ";
      cin >> x;
      cout << "Enter the Y number : ";
      cin  >> y;
    
      for (int a =1; a<=y; a++)
      {
          sum = sum * x;
      }
    cout << sum << endl;
    
    cin >> hold;
    }
    Can anyone please give me hints to do x to the -y power?
    Thanks.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    rather than multiplying sum by x multiply sum by the inverse of x

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    how do i do the inverse of x?

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    1/x

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    yeah i know its 1/x but how do i do on c++?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Please help me? How do i do this program?

  7. #7
    Shadow12345
    Guest
    Unless this assignment specifically needs you to perform your own power calculations simply include math.h to your project and use pow(x, y) where it calculates the value of 'x' raised to the 'y' power.

    The inverse of a number is the number you can multiply by in order to get 1.

    x * (x/x^2) = 1;

    EDIT:
    1/x is the easier and better way to find the inverse
    Last edited by Shadow12345; 12-20-2002 at 02:35 PM.

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by sonict
    yeah i know its 1/x but how do i do on c++?
    Why do you think it would be any different?

    1.0 / x

  9. #9
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I have this code to find the power of x^y. i already found out how to do x^y but am having trouble doing x^-y (x to the -y power)
    Simply do a check if y is greater or less than 0 and use the right method (the one you created or the 1/X method). And if you add a check for y == 0 (easy, returns 1), you will have a complete power function for integral values.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Can anyone please give me hints to do x to the -y power?

    Realize that

    x^(-y) = 1 / (x^y)

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, 02:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM