Raising an Integer to a power
Hello, I'm currently writing a program that is supposed to raise an integer to a power. However, the output keeps printing as going to infinity rather than the actual number when using the test results. I tried changing the double type to a long double, but it still is not large enough. Earlier when I was working on the program though, it did not seem to have this problem.
Any help at all would be greatly appreciated.
Thank you.
EDIT: The iterative function on the bottom does not work. I have not been able to finish it due to trying to stop the result from printing as 1.#NF00.
Code:
#include "stdafx.h"//standard library inclusion
#include "stdio.h"
#include "conio.h"
#include "time.h"
clock_t start,stop;//built in type for processor time (ticks)
double duration;//Records the run time, in seconds, of a function
long double Recur(double base, int pow);//Declaration of function for recursion.
long double Expon(double base, int pow);//Declaration of function for N-1 loop.
long double Iter(double base, int pow);//Declaration of function using iteration.
int main(void)
{
//This program raises a given integer (base) to a power (pow)
int pow=0, algo, test=0;//algo will be used to initiate the chosen algorithm. Test for choosing whether or not to run the test cases.
double base;
long double result;//Stored as a long double as in the test files the number is very large
printf("If you want to run Algorithm 1: Enter 1\nIf you want to run Algorithm 2(recursive): Enter 2\nIf you want to run Algorithm 3(iterative): Enter 3\n");
scanf("%d", &algo);//Choose which algorithm to run.
printf("If you want to run the test cases: Enter 1: ");
scanf("%d", &test);//Choose whether or not to run the test cases.
if(test!=1){//If Not running test cases, user inputs own base and power.
printf("Please enter your base: ");
scanf("%lf", &base);
printf("Please enter your power: ");
scanf("%d",&pow);//take in input for functions.
if(algo=1){//Will run algorithm one and clock time
start=clock();
result=Expon(base,pow);
stop=clock();
}
else if(algo=2){//Will run algorithm two and clock time
start=clock();
result=Recur(base,pow);
stop=clock();
}
else{//Will run algorithm three and clock time
start=clock();
result=Iter(base,pow);
stop=clock();
}
printf("%llf to the power of %d = %llf.\n",base, pow, result);//Print out results of chosen algorithm
duration = ((double)(stop - start))/CLK_TCK; //Print out duration of algorithm time
printf("Duration = %llf",duration);
}
else{//Run Test cases
base=1.0001;
int arraypow[8]= {1000,5000,10000, 20000, 40000, 60000, 80000, 100000};//Go through the array of powers in the test cases
int i=0;
for(i=0;i<8;i++){//Use loop to go through the test cases
if(algo=1){//Test cases for algorithm 1
pow=arraypow[i];
start=clock();
result=Expon(base,pow);//Function call for algorithm 1
stop=clock();
printf("%lf to the power of %d = %Lf.\n",base, pow, result);//Print out results as each power is used.
duration = ((double)(stop - start))/CLK_TCK; //Calculate duration of algorithm
printf("Duration = %lf\n",duration);
}
else if(algo=2){//Test cases for algorithm 2 (recursive)
pow=arraypow[i];
start=clock();
result=Recur(base,pow);//Function call for algorithm 2 Recursive
stop=clock();
printf("%lf to the power of %d = %Lf.\n",base, pow, result);//Print out results as each power is used.
duration = ((double)(stop - start))/CLK_TCK; //Calculate duration of algorithm
printf("Duration = %lf",duration);
}
else{//Test cases for algorithm 2 (iteration)
pow=arraypow[i];
start=clock();
result=Iter(base,pow);//Function call for algorithm 2 Iterartive
stop=clock();
printf("%lf to the power of %d = %Lf.\n",base, pow, result);//Print out results as each power is used.
duration = ((double)(stop - start))/CLK_TCK; //Calculate duration of algorithm
printf("Duration = %lf",duration);
}
}
}
getch();//Stop prgram from exiting after executing
getch();
return 0;
}
long double Expon(double base, int pow)//Function for algorithm 1
{
double result;
if(pow==0)
result = 1;
else if(pow==1)
result = base;
for(pow;pow>2;pow--){//Loop for N-1
base=base*base;
result=base;//Update result as power increases
}
return result;
}
long double Recur(double base, int pow)//Function for algorithm 2 (Recursive)
{
double result;
if(pow==0)
return result = 1;//Case for when power is equal to 0.
else if(pow==1)
return result = base;//Case for when power is equal to 1
else if(pow%2==0){
return Recur(base*base,pow/2);//Case for if pow is even
}
else
return Recur(base*base,pow/2)*base;//Case for if pow is odd
}
long double Iter(double base, int pow)//Function for algorithm 2 (Iterative)
{
double result;
while(pow>1){
if (pow%2==0){
base=(base*base);
pow=pow/2;
result=base;
}
else{
base=base*base;
pow= pow/2 +1;
result=base;
}
}
return result;
}