2016-11-08 25 views
-1

嗨,我已經查看了本網站上關於斯特林的近似值的其他問題,但沒有一個有幫助。我想要計算兩個斯特林近似方程的階乘和近似因子。然後,如果輸入小於或等於14,我將它們放在一個包含用戶輸入的所有值的表格中。或者,我將它們放在不同表格中,其中包含用戶輸入的近似值的所有值我已經嘗試了很多東西,但我不知道自己出錯的地方。計算斯特林在C中的逼近

我只是想知道如何獲得近似值的正確值?我已經得到了階乘。

這裏是整個代碼:

#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 
     } 

任何幫助獲得正確的近似值將不勝感激。

+0

'INT stirling_ans1;' - >'雙stirling_ans1;'(各個地方)。爲什麼要將一個'double'結果賦給一個'int'?如果代碼需要舍入,使用'round()'或'rint()'等。 – chux

+0

'1 /(12 * i)'是'int'數學。建議'1.0 /(12 * i)' – chux

+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);