ihave written a program that will give the root to a given power of a given number ie cube root of 125 is 5.
that all works. i then decided that i didnt want a lot of trailing zeros so wrote the following
Code:
int main()
{
//declare variables
double iBaseNum = 311.1696, iPowerNum = 0.25, iResult;
iResult = Power( iBaseNum, iPowerNum );
printf("%f to the power %f = ", iBaseNum, iPowerNum);
PrinttoScreen( iResult );
return 0;
}
double Power( double x, double y )
{
static int itmpPower = 0, i = 0;
static double iBase = 1, presision = 0.000001;
if ( y == 0 )
{
//anything to the power zero is 1 so return 1
return 1;
}//*
else if ( y < 1 && y > 0 )
{
itmpPower = 1 / y;
while ( i <= x )
{
iBase += 1;
i = Power( iBase, itmpPower );
}
iBase -= 1;
if ( x != Power( iBase, itmpPower ) )
{
printf("this run!\n");
while ( Power( iBase, itmpPower ) < x )
{
iBase += presision;
}
}
return iBase;
}//*/
else if ( y == 1 )
{
//end of line so return base
return x;
}
return x * Power( x, --y );
}
void PrinttoScreen ( double iResult )
{
int x, iCount = 0;
double itmpResult;
x = iResult;
itmpResult = iResult;
while ( x < itmpResult )
{
if ( iCount == 9 )
{
break;
}
itmpResult *= 10;
x = itmpResult;
iCount += 1;
}
switch ( iCount )
{
case 0:
printf("%d\n", x);
break;
case 1:
printf("%0.1f\n", iResult);
break;
case 2:
printf("%0.2f\n", iResult);
break;
case 3:
printf("%0.3f\n", iResult);
break;
case 4:
printf("%0.4f\n", iResult);
break;
case 5:
printf("%f0.5\n", iResult);
break;
case 6:
printf("%f0.6\n", iResult);
break;
case 7:
printf("%f0.7\n", iResult);
break;
case 8:
printf("%f0.8\n", iResult);
break;
case 9:
printf("%f0.9\n", iResult);
break;
}
}
if i enter 311.1696 to the power 0.25 (4th root) i get 4.2000000.9 not 4.2 i get there is prob some rounding errors in the registers etc however why is there 2 decimal places?