Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中逼近无穷级数中的e_C_Math_Loops_Recursion_Infinite - Fatal编程技术网

如何在C中逼近无穷级数中的e

如何在C中逼近无穷级数中的e,c,math,loops,recursion,infinite,C,Math,Loops,Recursion,Infinite,所以我试着解决这个问题: 然而,我不完全确定从哪里开始,或者我到底在寻找什么 另外,有人告诉我应该给程序输入,比如:零(0)、非常小(0.00001)和不太小(0.1) 我得到的是:作为参考,但这个公式看起来与问题中的公式不完全一样 最后,我被告知程序的输入是一个小的数字ε。例如,您可以假设为0.00001f 继续添加无穷级数,直到当前项的值低于ε 但总而言之,我不知道那是什么意思。我有些理解维基上的等式。但是,我不确定从哪里开始解决给定的问题。看看它,有人知道我应该在C中使用什么样的公式,什

所以我试着解决这个问题:

然而,我不完全确定从哪里开始,或者我到底在寻找什么

另外,有人告诉我应该给程序输入,比如:零(0)、非常小(0.00001)和不太小(0.1)

我得到的是:作为参考,但这个公式看起来与问题中的公式不完全一样

最后,我被告知程序的输入是一个小的数字ε。例如,您可以假设为0.00001f

继续添加无穷级数,直到当前项的值低于ε

但总而言之,我不知道那是什么意思。我有些理解维基上的等式。但是,我不确定从哪里开始解决给定的问题。看看它,有人知道我应该在C中使用什么样的公式,什么是“E”,以及它在哪里起作用(即,在公式中,我理解它应该是用户输入)

到目前为止的代码

#include <stdio.h>
#include <math.h>

//Program that takes in multiple dates and determines the earliest one
int main(void)
{
    float e = 0;
    float s = 0;
    float ct = 1;
    float ot= 1;
    int n = 0;
    float i = 0;
    float den = 0;
    int count = 0;

    printf("Enter a value for E: ");
    scanf("%f", &e);

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


    for(n = 0; ct > e; n++)
    {
        count++;
            printf("The value of the current term is: %f", ct);

        printf("In here %d\n", count);

        den = 0;

        for(i = n; i > 0; i--)
        {
            den *= i;
        }

        //If the old term is one (meaning the very first term), then just set that to the current term
        if (ot= 1)
        {
            ct = ot - (1.0/den);
        }
        //If n is even, add the term as per the rules of the formula
        else if (n%2 == 0)
        {
            ct = ot + (1.0/den);
            ot = ct;
        }
        //Else if n is odd, subtract the term as per the rules of the formula
        else
        {
            ct = ot - (1.0/den);
            ot = ct;
        }

        //If the current term becomes less than epsilon (the user input), printout the value and break from the loop
        if (ct < epsilon)
        {
            printf("%f is less than %f",ct ,e);
            break;
        }
    }

    return 0;
}

因此,根据每个人的评论,并使用维基百科中的第四个“疯狂”等式,就像我被告知的那样,这就是我提出的代码。我脑子里的逻辑似乎与大家一直在说的是一致的。但是,我的产出根本不是我想要达到的。有人知道我可能做错了什么吗?

这个求和符号给了你一个线索:你需要一个循环

什么是
0?1,当然。因此,e的起始值是1

接下来,您将为n编写一个从1到某个较大值的循环(无穷大可能意味着一个while循环),在此计算每个连续项,查看其大小是否超过ε,并将其添加到e的和中

当项小于ε时,停止循环

现在不要担心用户输入。让你的功能正常工作。硬编码一个epsilon,看看当你改变它时会发生什么。保留最后一位的输入

你需要一个好的阶乘函数。(不是真的-谢谢Mat提醒我。)

你问过常数e是从哪里来的吗?那这个系列呢?该级数是指数函数的泰勒级数展开式。请参阅任何介绍微积分的文本。常数e是指数为1的简单指数函数

我有一个很好的Java版本在这里工作,但我不打算发布它。它看起来就像C函数一样,所以我不想泄露它

更新:既然你已经展示了你的,我将展示我的:

package cruft;

/**
 * MathConstant uses infinite series to calculate constants (e.g. Euler)
 * @author Michael
 * @link
 * @since 10/7/12 12:24 PM
 */
public class MathConstant {

    public static void main(String[] args) {
        double epsilon = 1.0e-25;
        System.out.println(String.format("e = %40.35f", e(epsilon)));
    }

    // value should be 2.71828182845904523536028747135266249775724709369995
    //          e =    2.718281828459045
    public static double e(double epsilon) {
        double euler = 1.0;
        double term = 1.0;
        int n = 1;
        while (term > epsilon) {
            term /= n++;
            euler += term;
        }
        return euler;
    }
}

但是如果你需要一个阶乘函数,我会推荐一个表、记忆和gamma函数,而不是简单的student实现。如果你不知道这些是什么,用谷歌搜索。祝你好运。

这个求和符号给了你一个线索:你需要一个循环

什么是
0?1,当然。因此,e的起始值是1

接下来,您将为n编写一个从1到某个较大值的循环(无穷大可能意味着一个while循环),在此计算每个连续项,查看其大小是否超过ε,并将其添加到e的和中

当项小于ε时,停止循环

现在不要担心用户输入。让你的功能正常工作。硬编码一个epsilon,看看当你改变它时会发生什么。保留最后一位的输入

你需要一个好的阶乘函数。(不是真的-谢谢Mat提醒我。)

你问过常数e是从哪里来的吗?那这个系列呢?该级数是指数函数的泰勒级数展开式。请参阅任何介绍微积分的文本。常数e是指数为1的简单指数函数

我有一个很好的Java版本在这里工作,但我不打算发布它。它看起来就像C函数一样,所以我不想泄露它

更新:既然你已经展示了你的,我将展示我的:

package cruft;

/**
 * MathConstant uses infinite series to calculate constants (e.g. Euler)
 * @author Michael
 * @link
 * @since 10/7/12 12:24 PM
 */
public class MathConstant {

    public static void main(String[] args) {
        double epsilon = 1.0e-25;
        System.out.println(String.format("e = %40.35f", e(epsilon)));
    }

    // value should be 2.71828182845904523536028747135266249775724709369995
    //          e =    2.718281828459045
    public static double e(double epsilon) {
        double euler = 1.0;
        double term = 1.0;
        int n = 1;
        while (term > epsilon) {
            term /= n++;
            euler += term;
        }
        return euler;
    }
}

但是如果你需要一个阶乘函数,我会推荐一个表、记忆和gamma函数,而不是简单的student实现。如果你不知道这些是什么,用谷歌搜索。祝你好运。

你需要写一个C开头的程序。互联网上有很多关于这方面的资源,包括如何从argc和argv变量获取用户输入。如果未输入epsilon,看起来您将使用0.00001f表示epsilon。(在尝试让程序接受输入之前,使用该选项使程序工作。)

为了计算级数,您将使用一个循环和一些变量:sum、current_term和n。在每个循环迭代中,使用n,增量n计算当前_项,检查当前项是否小于ε,如果不小于ε,则将当前_项添加到总和中


这里要避免的最大陷阱是错误地计算整数除法。例如,您将希望避免像1/n这样的表达式。如果要使用这样的表达式,请改用1.0/n。

您需要编写一个C开头的程序。互联网上有很多关于这方面的资源,包括如何从argc和argv变量获取用户输入。如果未输入epsilon,看起来您将使用0.00001f表示epsilon。(在尝试让程序接受输入之前,使用该选项使程序工作。)

为了计算级数,您将使用一个循环和一些变量:sum、current_term和n。在每个循环迭代中,使用n,增量n计算当前_项,检查当前项是否小于ε,如果不小于ε,则将当前_项添加到总和中


这里要避免的最大陷阱是错误地计算整数除法。例如,您将希望避免像1/n这样的表达式。如果你要使用这样一个表达式,就用1.0/n来代替。

事实上,这个程序与Deitel在C语言学习编程中给出的程序非常相似,现在说到这里(错误不能是0,因为e是一个无理数,所以不能精确计算)。我这里有一个代码,可能对y非常有用
#include <stdio.h>


/* Function Prototypes*/
long double eulerCalculator( float error, signed long int *iterations );
signed long int factorial( int j );


/* The main body of the program */
int main( void ) 
{
    /*Variable declaration*/
    float error;
    signed long int iterations = 1;

    printf( "Max Epsilon admited: " );
    scanf( "%f", &error );
    printf( "\n The Euler calculated is: %f\n", eulerCalculator( error, &iterations ) ); 
    printf( "\n The last calculated fraction is: %f\n", factorial( iterations ) );
    return 1;
}


long double eulerCalculator( float error, signed long int *iterations ) 
{
    /* We declare the variables*/
    long double n, ecalc;


    /* We initialize result and e constant*/
    ecalc = 1; 

    /* While the error is higher than than the calcualted different keep the loop */
    do {

        n = ( ( long double ) ( 1.0 / factorial( *iterations ) ) );
        ecalc += n;
        ++*iterations;

    } while ( error < n );


    return ecalc;
}

signed long int factorial( signed long int j )
{
    signed long int b = j - 1;

    for (; b > 1; b--){
        j *= b;
    }

    return j;
}
unsigned int n = 0; // Iteration.  Start with n=0;
double fact = 1;    // 0! = 1.  Keep running product of iteration numbers for factorial.
double sum = 0;     // Starting summation.  Keep a running sum of terms.
double last;        // Sum of previous iteration for computing e
double e;           // epsilon value for deciding when done.
do {
    last = sum;
    sum += 1/fact;
    fact *= ++n;
} while(sum-last >= e);
            (n!)/(2n+1)!  (from n=1 to infinity)