/* 14jan06 john pfeiffer returns the result of a base to an exponent */
#include <stdio.h>
#include <stdlib.h>
/* pow returns float (for neg values)
exponent function handles positive exp, exp=0, and even negative exponents
*/
float pow(int x, int y) /* the classic x to the y power */
{ int i = 1;
float result = 1; /* prepares to return 1 if x is to the power of 0 */
short negexp = 0;
if( y != 0 ) /* special case - no work just return the init of 1 */
{ if( y < 0 )
{ negexp = 1;
y = abs(y);
}
for( i=1; i<y+1; i++ )
{ result = result * (float)x; }
}
if( negexp == 1 )
{ result = ( 1 / result );
printf("\n%d:%.2f\n", y, result);
}
return result;
}/* end power function */
int main(void)
{
printf("0^0: %.2f \n",pow(0,0));
printf("1^0: %.2f \n",pow(1,0));
printf("2^1: %.2f \n",pow(2,1));
printf("2^10: %.2f \n",pow(2,10));
printf("2^-1: %.2f \n",pow(2,-1));
printf("3^-3: %.4f \n",pow(3,-3));
printf("2^8: %.2f \n",pow(2,8));
return 0;
}