试图找到 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;
}
- 有一种计算数字 x 的 n 次方根的数学算法,称为 Newton-Raphson 求根法。我试着编写这个算法。长话短说,我似乎得到了正确的答案,但是
问题 1:我卡在了 while 中,我不知道为什么
问题2:精度应该由变量epsilon决定,但答案总是小数点后5位。*
一个问题是要检查 epsilon,代码应该是
while (fabs(error) > epsilon) {
... improve ...
}
您正在检查下一个针对 epsilon 的近似值(也没有 fabs
)。
另一个问题是输出流在打印浮点值时使用固定的小数位数,如果你想增加你需要寻找 std::setprecision
(或者只使用 printf("%.18g\n", 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;
}
- 有一种计算数字 x 的 n 次方根的数学算法,称为 Newton-Raphson 求根法。我试着编写这个算法。长话短说,我似乎得到了正确的答案,但是 问题 1:我卡在了 while 中,我不知道为什么 问题2:精度应该由变量epsilon决定,但答案总是小数点后5位。*
一个问题是要检查 epsilon,代码应该是
while (fabs(error) > epsilon) {
... improve ...
}
您正在检查下一个针对 epsilon 的近似值(也没有 fabs
)。
另一个问题是输出流在打印浮点值时使用固定的小数位数,如果你想增加你需要寻找 std::setprecision
(或者只使用 printf("%.18g\n", x);
这是我个人喜欢做的)。