![](/img/trans.png)
[英]Problem with implementing successive approximation algorithm in C++ with Visual Studio 2008
[英]C++: Implementing a numeric approximation for ln(a)
我想實現公式:
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
int main(){
double a, eps = numeric_limits<double>::epsilon();
cout << "a=";
cin >> a;
double w = sqrt(a), prod = 1 + w;
int n = 1;
do {
w = sqrt(w);
prod *= 1 + w;
cout << "ln(" << a << ")=" << ldexp(a-1, n)/prod << endl;
n++;
} while(abs(w - 1) > eps);
return 0;
}
但例如 ln(2.78787)=0.512639 這不可能是真的。 錯誤在哪里?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.