0

我无法理解以下程序如何创建自然对数(我想知道是否可以)。我在博客上找到了它。如果它不产生自然对数,我将如何制作一个?

void main()
{
    int x,i,j;
    float sum=0,power=1;
    printf("enter x for sum upto 7th term: ");
    scanf("%d",&x);
    for(i=1;i<=6;i++) 
    { 
        power=1; 
        for(j=0;j<=i;j++) 
        { 
            power = power * ((x-1.0)/2.0); 
        } 

        sum = (1.0/2) * power + sum; 
    } 
    sum=sum + (float)(x-1.0)/x;
    printf("%f",sum); 
}
4

1 回答 1

1

该程序可能正在尝试计算自然对数,但它有很多问题。以下更正保持 OP 风格

(x > 0.5) 时 ln(x) 的公式如下

ln(x) = (x-1)/x + (1/2)((x-1)/x)^2 + (1/3)((x-1)/x)^3 + ...

void main() {
  int i, j;
  float sum = 0.0f;
  float power;
  float x;
  printf("enter x for sum up to 7th term: ");
  scanf("%f", &x);  // let x be a float rather than limited to int
  for (i = 1; i <= 7; i++) { // do all 7 terms here
    power = 1.0f;
    for (j = 0; j < i; j++) {
      power = power * ((x - 1.0f) / x); // need to /x not 2.0
    }
    sum += (1.0f / i) * power; // need to /i not 2.0
  }
  //sum = sum + (float) (x - 1.0f) / x;  not needed as done in above 1st iteration
  printf("ln(%f) = \n%f\n%lf\n", x, sum, log(x));
}

以下链接中的“以 1 为中心的泰勒系列”似乎是错误的,因为我怀疑它的术语应该有交替的符号。
http://math2.org/math/expansion/log.htm

于 2013-08-15T14:45:10.093 回答