I’m trying to write a recursive power function without using math.h / pow(), but I keep getting garbage values of -1.#IND. I think i’m missing something when I call for a new instance of the power function, but I don’t quite understand how to get around it. My full code is fairly short:
#include <iostream>
using namespace std;
double power(double b, int e){
double x;
if(e == 0){
x = b;
}
else if(b == 0){
x = 1;
e = 0;
}
else if(e < 0){
x = (1 / b) * power(b, ++e);
}
else{
x = b * power(b, --e);
}
return x;
}
int main(){
double num;
int exp;
cout << "Please Enter Your Number: ";
cin >> num;
cout << "Please Enter The Explonent: ";
cin >> exp;
cout << power(num, exp);
cin >> num;
}
The only place you return a value from
poweris wheree==0.The other cases compute values, but never return them.