计算斯特林在C中的逼近

时间:2016-11-08 21:42:08

标签: c factorial approximation

大家好,我已经查看了本网站关于斯特林近似的其他问题,但没有一个是有帮助的。我想要计算阶乘,并从两个斯特林的近似方程近似于阶乘。然后,如果输入小于或等于14,我将它们放在一个表格中,其中包含导致用户输入的所有值。或者我将它们放在一个不同的表格中,该表格包含所有近似值的值。用户的输入是5的倍数。我尝试了很多东西,但我不知道我哪里出错了。

我只是想知道如何获得近似值的正确值?我已经得到了阶乘。

以下是整个代码:

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

#define PI 3.141592653589793
#define e 2.71828
#define TRUE 1
#define FALSE 0

long long fact(int i);
double stirling1(int i);
double stirling2(int i);

/*--------------------------- main function -------------------------------
Purpose: The purpose of this program is to give the user the value of the 
factorial of the nonnegative integer the user inputs.
---------------------------------------------------------------------------*/

int main ()
{
char ch = 'y';
int flag, again;
int n;
int i;

again = TRUE;

while ( again == TRUE )
{
    printf("Please enter a nonnegative integer:\n"); //ask the user for the input
    flag = scanf("%d", &n);

    if ( flag != 1 )
    {
        while ((getchar()) != '\n');
    }

    if ( flag != 1 ) //run this statement if the user inputs a noninteger
    {
        printf("Input must be an integer.\n");
        continue;
    }

    else if ( n < 0 ) //run this statement if the user inputs a negative integer
    {
        printf("The factorial is undefined for negative arguments.\n");
        continue;
    }

    else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
    {
        printf("Number     Factorial    Approximation          Approximation2\n------------------------------------------------------------------\n"); //prints the header for first table

        for ( i = 1; i <= n; ++i )
            {
                printf("%d     %14lld     %e           %e\n", i, fact( i ), stirling1( i ), stirling2( i )); //calls functions to input factorials
            }
    }

    else //run this statement if the user inputs a number greater than 14
    {
        printf("Number   Approximation1         Approximation2\n-----------------------------------------------------\n"); //prints the header for second table

        for ( i = 1; i != n; ++i )
        {
            i *= 5;
            printf("%d        %e            %e\n", i, stirling1( i ), stirling2( i )); //calls functions to input approximate factorials
        }
    }

    printf("Do you want to compute another factorial? (y/n):"); //ask user if they want another factorial of a different number
    scanf(" %c", &ch);

    if (ch != 'y') 
        again = FALSE; //if user does not input 'y' then do not compute another factorial
}

printf( "**** Program Terminated ****\n" ); //ends program

}

long long fact( int i ) //function to find exact factorial
{
if (i <= 1) 
{
    return 1;
}

else 
{
    return i * fact(i-1);
} //return exact factorial to main
}

double stirling1( int i ) //function to find first approximate factorial
{
int stirling_ans1;

stirling_ans1 = pow( i , i ) * sqrt( 2.0 * PI * i) * exp(-i); //equation to find first approximate factorial

return stirling_ans1; //return approximate factorial to main
}

double stirling2( int i ) //function to find second approximate factorial
{
int stirling_ans2;

stirling_ans2 = pow( i, i ) * pow( e, -i ) * sqrt( 2 * PI * i) * ( 1 + ( 1 / (12 * i) ));
//equation to find second approximate factorial

return stirling_ans2; //return approximate factorial to main
}

以下是两个近似函数的代码:

double stirling1( int i ) //function to find first approximate factorial
{
int stirling_ans1;

stirling_ans1 = pow( i , i ) * sqrt( 2.0 * PI * i) * exp(-i); //equation to find first approximate factorial

return stirling_ans1; //return approximate factorial to main
}

double stirling2( int i ) //function to find second approximate factorial
{
int stirling_ans2;

stirling_ans2 = pow( i, i ) * pow( e, -i ) * sqrt( 2 * PI * i) * ( 1 + ( 1 / (12 * i) ));
//equation to find second approximate factorial

return stirling_ans2; //return approximate factorial to main
}

在主函数中实现近似函数:

else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
    {
        printf("Number     Factorial    Approximation          Approximation2\n------------------------------------------------------------------\n"); //prints the header for first table

        for ( i = 1; i <= n; ++i )
            {
                printf("%d     %14lld     %e           %e\n", i, fact( i ), stirling1( i ), stirling2( i )); //calls functions to input factorials
            }
    }

    else //run this statement if the user inputs a number greater than 14
    {
        printf("Number   Approximation1         Approximation2\n-----------------------------------------------------\n"); //prints the header for second table

        for ( i = 1; i != n; ++i )
        {
            i *= 5;
            printf("%d        %e            %e\n", i, stirling1( i ), stirling2( i )); //calls functions to input approximate factorials
        }

任何获得正确近似值的帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

OP代码在至少2个位置执行弱或不正确的数学运算。

1 / (12 * i)int数学而不是所需的FP 1.0 / (12 * i)

通过int传递代码“回合”。然而,这充其量只能截断为0.0。最好使用double stirling_ans1;而不是int stirling_ans1;

次要:
不清楚为什么代码使用pi和e的粗略近似值。也许:

#define PI 3.1415926535897932384626433832795
#define e 2.7182818284590452353602874713527

或者更好的是,从一次性计算中形成值:

double pi = acos(-1.0);
double e = exp(1.0);