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.
Printable View
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.
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;
}
Within reason, you can also unroll the multiplications.
Of course with variable exponents you will need to use the pow() functions.Code:int x, y;
x = pow(15, 3); //this works
y = 15*15*15; //this is faster
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;
}
>y = 15*15*15; //this is faster
Maybe, but what about:
3^25
or
7^2.6
2^3 doesn't work with me!
Maybe I need to overload it.