我在 Visual Studio 2012 中编写了幂函数代码:
#include<stdio.h>
long double myPow( long double base, unsigned int exp);
int main(){
long double base, m;
unsigned int exp;
scanf("%Lf%u",&base ,&exp);
m=myPow(base, exp);
printf("%.3Lf",m);
//getch();
return 0;
}
long double myPow( long double base, unsigned int exp){
static long double power=1;
if (base==1)
return base;
if(exp==1)
return power*base;
else{
power*=base;
myPow(base,exp-1);
}
}
但 vs 中的输出始终是-1.#IO。
问题是什么?
最佳答案
您需要返回 myPow(base, exp-1)
。
这是计算幂的一种非常糟糕的方法。查找涉及平方的算法。
关于c++ - 在 Visual Studio 中返回长 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137228/