Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/math/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在试图找到x的第n个根时陷入无限循环 双幂(双基,整数指数){ //仅就上下文而言,double大于long int //也可编程为假定根为非十进制、非负输入的方法 双答案=基数; 如果(指数=0){ 返回1.0; } 如果(指数>0),则为else{ 对于(inti=1;i_C++_Math_Root - Fatal编程技术网

C++ 在试图找到x的第n个根时陷入无限循环 双幂(双基,整数指数){ //仅就上下文而言,double大于long int //也可编程为假定根为非十进制、非负输入的方法 双答案=基数; 如果(指数=0){ 返回1.0; } 如果(指数>0),则为else{ 对于(inti=1;i

C++ 在试图找到x的第n个根时陷入无限循环 双幂(双基,整数指数){ //仅就上下文而言,double大于long int //也可编程为假定根为非十进制、非负输入的方法 双答案=基数; 如果(指数=0){ 返回1.0; } 如果(指数>0),则为else{ 对于(inti=1;i,c++,math,root,C++,Math,Root,一个问题是要检查epsilon,代码应该是 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;

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

    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检查下一个近似值(也没有
fabs


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

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

    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检查下一个近似值(也没有
fabs


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

我们需要
power
来获得一个最小的可复制示例,否则代码将无法编译。好的@LouisGo将添加您也可以在循环中提取您的条件作为变量,并放置断点以查看其不起作用的原因。例如:while(next_iter_ans>epsilon){…nextIterAns=您的条件}如果
左边的项变为负数,它将保持为负数。通常Newton Raphson技术比较绝对值,因此总是比较两个正值(希望其中一个收敛到零,因此最终将小于
ε
)但是,根据输入的性质和舍入误差,收敛永远不会发生是完全可行的。因此,通常(尽管不是普遍的)也会限制迭代次数,这将捕获(绝对)的情况被测试的值永远不会低于
epsilon
。我们需要
power
来获得一个最小的可复制示例,否则代码将无法编译。好的@LouisGo将添加另外,您可以在循环中提取您的条件作为变量,并放置断点以查看其不起作用的原因。例如:while(下一步>epsilon){….nextIterAns=your condition}如果
左边的项变为负值,它将保持为负值。通常Newton Raphson技术比较绝对值,因此总是比较两个正值(希望其中一个收敛到零,因此最终将小于
ε
)然而,根据输入的性质和舍入误差,收敛永远不会发生是非常可行的。因此,通常(尽管不是普遍的)也会限制迭代次数,这将捕获被测试的(绝对)值永远不会低于ε的情况。