raising to a power

This is a discussion on raising to a power within the C++ Programming forums, part of the General Programming Boards category; Does anyone know of a way to raise a constant to a variable. Ex. 15 raised to the (xy) Im ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Question raising to a power

    Does anyone know of a way to raise a constant to a variable.
    Ex. 15 raised to the (xy)
    Im kinda new to this so any help would be much appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    #include <cmath>
    #include <iostream>
    
    int main(){
         int xy = 2;
         int x = pow(3, 4);   //this means 3 to the fourth
         int y = pow(15, xy); //15 to the xy
         return 0;
    }

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Within reason, you can also unroll the multiplications.

    Code:
    int x, y;
    x = pow(15, 3);  //this works
    y = 15*15*15;   //this is faster
    Of course with variable exponents you will need to use the pow() functions.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    wll if you want to write a function then this is how it works




    Code:
    power(x,y)
    {
    
    double temp=1;
    for(int i=0;i<y+1;i++)
    {
    
    temp=temp*x;
    
    }
    
    return temp;
    }

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >y = 15*15*15; //this is faster

    Maybe, but what about:

    3^25

    or

    7^2.6
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    2^3 doesn't work with me!
    Maybe I need to overload it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with checking, raising to the power of 2
    By philippe in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 07:30 PM
  2. Replies: 6
    Last Post: 11-08-2005, 03:05 PM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 08:38 AM
  4. Raising to a power
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2001, 05:44 PM

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