[英]C++: Implementing a numeric approximation for ln(a)
I want to implement the formula:我想实现公式:
#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;
}
But eg ln(2.78787)=0.512639 and that cannot be true.但例如 ln(2.78787)=0.512639 这不可能是真的。 Where is the mistake?
错误在哪里?
Initial conditions for the loop are off.循环的初始条件为关闭。 Start with
从...开始
double w = a, prod = 1;
and the program would produce expected results该计划将产生预期的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.