试图找到 x 的第 n 个根时陷入无限循环

Stuck in infinite loop while trying to find nth root of x

    double power(double base, int exponent){
        //Just for context, a double is larger than a long long int
        //Also method programmed to assume non-decimal, non-negative input for root
        double answer = base;
        if(exponent == 0){
            return 1.0;
        }
        else if(exponent > 0){
            for(int i=1; i<exponent; i++){
                answer*=base;
            }
            return answer;
        }
        else{//if exponent is negative
            for(int i=1; i<exponent*(-1); i++){
                answer*=base;
            }
            return 1/answer;
        }
    }
    double newtonRaphsonRoot(double base, int root){//FOR FIDING ROOTS OF CONSTANT #s
        if(base == 1){
            return 1.0;
        }
        //Formula: x1 = x0 - f(x0)/f'(x0)
        //--------------------------------
        //Also method programmed to assume non-negative integer input for root
        double epsilon=0.01;//accuracy
        double answer = base;//answer starts off as initial guess and becomes better approximated each iteration
        if(base > 1){
            answer=base/2;
        }
        while( answer - ( power(answer,root) - base)/(root*power(answer,root-1) ) > epsilon){
        //Formula: x1 = x0 - f(x0)/f'(x0). Continue formula until error is less than epsilon
            answer = answer - ( power(answer,root) - base)/(root*power(answer,root-1) );
            std::cout<<"Approximation: answer = "<< answer <<"\n";
        }
        return answer;
    }

一个问题是要检查 epsilon,代码应该是

while (fabs(error) > epsilon) {
    ... improve ...
}

您正在检查下一个针对 epsilon 的近似值(也没有 fabs)。

另一个问题是输出流在打印浮点值时使用固定的小数位数,如果你想增加你需要寻找 std::setprecision (或者只使用 printf("%.18g\n", x);这是我个人喜欢做的)。