【问题标题】:Finding approximate value of 'e' in C language在 C 语言中找到 'e' 的近似值
【发布时间】:2018-06-17 15:32:54
【问题描述】:

我正在尝试使用 C 程序找到“e”的近似值。我正进入(状态 无论我输入 n 的值,e 的值都为真。请让我 在我的代码中找到错误。

#include <stdio.h>

int f;
int k;

int factorial (int f);

int main () { 
    int n,i;

    int e = 1;

    printf("Enter the value of n:");
    scanf("%d",&n);

    for (i = 1; i <= n; i++) {
        e = e + (1/factorial(i));
    }

    printf("The value of e is %d",e);

    return(0);
}

int factorial (int f) {    
    for (k = 1; k <= f; k++) {
       f = f*k;
    }

    return(p);
}

【问题讨论】:

  • 你知道e 包含很多小数吗? integer 变量怎么会有小数呢?
  • 顺便说一下,您展示的代码甚至不会构建。请花一些时间阅读the help pages,了解how to ask good questions,并了解如何创建Minimal, Complete, and Verifiable Example
  • 另外,您的阶乘定义不正确。它会乘以 * f 两次。
  • 1/factorial(i)整数除法,结果为 0(1 除外!),即使您有 double e
  • @VictoriaRuiz:比两次更糟糕:它移动循环的速度比赶上它的速度快。

标签: c numerical-methods


【解决方案1】:

如前所述,您必须使用浮点变量。还有一个问题是12之后阶乘函数会溢出!此外,不断地重新计算阶乘是一种处理能力的浪费。这显示了在前项的基础上实现泰勒级数的更有效方法。这也回避了计算阶乘的问题。

#include <stdio.h>

int main(void) {
    double e, term;
    int t, n;

    printf("Enter the value of n:");
    if(scanf("%d", &n) != 1) {
        return 1;
    }

    e = 1;
    term = 1;
    for(t = 1; t <= n; t++) {
        term /= t;
        e += term;
    }
    printf("%.15f\n", e);
    return 0;
}

程序输出使用您的程序可以处理的最大n

输入 n:12 的值
2.718281828286169

而在math.h 中是

2.718281828459045...

这不是很准确。这个例子可以得到更准确的值:

输入 n:20 的值
2.718281828459046

【讨论】:

    【解决方案2】:
    double factorial (int f) {
        double p = 1;
        for (k = 1; k <= f; k++) {
            p = p*k; // f = f * k;
        }
    
        return p; // what is p doing here? (in original code)
    }
    

    您的问题是您正在更改 f 的值,除了已经解释了其他错误。它应该返回double 和其他一些错误。

    【讨论】:

      【解决方案3】:

      您有一些错误。您在int 变量中添加浮动值!请阅读以下代码的cmets:

      int k; // remove f
      
      int factorial (int f);
      
      int main () {
      
      int n,i;
      
      float e=1; // take e as float/double, not int
      
      printf("Enter the value of n:");
      
      scanf("%d",&n);
      
      for (i=1 ; i<=n ; i++) {
      
          e=e+(1.0/factorial(i)); // take 1.0 (floating value) instead of 1 (integer value)
      }
      
      printf("The value of e is %f",e);
      
      return(0);
      }
      
      int factorial (int f) {
          int total=1; // initialize total with 1
          for (k=1 ; k<=f ; k++) {
      
              total=total*k; // here is a big mistake in your code. You incremented value of f in every iteration. Try to print value of f after every iteration to find your error.
          }
      
          return(total);
      }
      

      【讨论】:

        【解决方案4】:

        对于准确的float 估计,

        e = (((((((((1.f / 11 + 1) / 10 + 1) / 9 + 1 ) / 8 + 1) / 7 + 1) / 6 + 1) / 5 + 1) / 4 + 1) / 3 + 1) / 2 + 2
        

        初始的.f 是必不可少的。


        最好从最小值开始累加。

        【讨论】:

          猜你喜欢
          • 2011-10-22
          • 2013-12-08
          • 2013-10-13
          • 2018-05-23
          • 2015-07-20
          • 2017-07-10
          • 1970-01-01
          • 1970-01-01
          • 2015-03-01
          相关资源
          最近更新 更多