c++ - 在 Visual Studio 中返回长 double 值

标签 c++ c

我在 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/

相关文章:

c++ - 如何打包python模块依赖的共享对象?

c++ - 如何重载流运算符以使用位于不同文件中的函数?

c - 如何使结构数组成为单个字符串

C 编程、文件、链表

c++ - 执行 1<<40 时移位计数溢出

c++ - 枚举类型的运算符重载

c++ - 单独的类文件错误?

c - 在 QNX 上调试宏相关错误

c - 从 Lua 调用的推送表的 C 函数应该返回什么?

c++ - 在 sizeof 命令的 extern 语句中指定 char 数组大小