I wrote this version of "pow" for floating point exponents. I know that it's not the best one ever written but I just want it to work.
When I write as input "2^1.23", for example, it enters an infinite recursion.
Debugging it I discovered that at a certain point it has to do "2^3" and when it arrives at "n2" the difference between "exp" and "n1" is 2.38419e-06 instead of 0.
#define MAX_VALUE 1000000
double Pow (double base, double exp)
{
int i = 0;
double pot = 0.0;
long n1 = long(exp); //I take the integer part of the exponent.
float n2 = fabs((exp - n1)*MAX_VALUE);
while (int(n2) % 10 == 0 && n2 != 0) { //I take the decimal part of the exponent.
n2 /= 10;
}
double n3 = fabs(exp*MAX_VALUE);
while (long(n3) % 10 == 0) { //I delete any eventual point to obtain an integer.
}
if (exp*MAX_VALUE/n3 < MAX_VALUE) { //Characteristic of all floating point numbers: when multiplied by 10^n they will have less zeros then 'n'.
long n4 = fabs(float(n3/exp)); //I find the power of 10 by which multiply the original exponent to obtain an integer.
short n5 = fabs(short(n2)/10); //I find the second to last digit of the fractional part.
double n6 = fabs(int(n2) % 10); //I find the last digit of the fractional part.
if (fabs(n2) < 11) {
pot = base; //I set 'x' of zero equal to the base of the original power.
while (fabs(Pow(pot, n4) - Pow(base, n3)) > 0.000001) { //I set the precision of the result.
pot -= ((Pow(pot, n4) - Pow(base, n3))/(n4*(Pow(pot, (n4 - 1))))); //I calculate the nth root with the Newton algorithm.
}
} else {
double a1 = Pow(base, n1);
double a2 = Pow(base, 0.1);
double a3 = Pow(base, n2/n4-0.1);
pot = a1*a2*a3;
}
return (exp > 0) ? pot : 1/pot;
}
}