2016-11-08 7 views
-1

Salut, donc j'ai regardé les autres questions sur ce site concernant l'approximation de Stirling mais aucun d'entre eux ne m'a été utile. Je suis supposé calculer la factorielle et aussi approximer la factorielle à partir des deux équations d'approximation de Stirling. Ensuite, je les mets dans un tableau de toutes les valeurs menant à l'entrée de l'utilisateur si l'entrée est inférieure ou égale à 14. Ou je les mets dans un tableau différent de toutes les valeurs des approximations menant à l'entrée de l'utilisateur dans multiples de 5. J'ai essayé beaucoup de choses mais je ne sais pas où je me suis trompé.Calcul de l'approximation de Stirling en C

Je me demande simplement comment obtenir les valeurs correctes pour les approximations? J'ai déjà eu le factorial.

Voici le code complet:

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

Voici le code pour les deux fonctions d'approximation spécifiquement:

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 
} 

Et où ont été mis en œuvre les fonctions d'approximation dans la fonction principale:

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 
     } 

Toute aide pour obtenir les approximations correctes serait appréciée.

+0

'int stirling_ans1;' -> '' deux stirling_ans1; (divers lieux). Pourquoi assigner un résultat 'double' à un' int'? Si le code doit arrondir, utilisez 'round()' ou 'rint()', etc. – chux

+0

'1/(12 * i)' est 'int' math. Suggérer '1.0/(12 * i)' – chux

+0

Merci beaucoup! Cela a réglé le problème. –

Répondre

2

Le code OP effectue des calculs faibles ou incorrects en au moins 2 endroits.

1/(12 * i) est int mathématiques plutôt que le FP nécessaire 1.0/(12 * i). Code "tours" en passant par un int. Pourtant, cela, au mieux, tronque vers 0.0. Mieux vaut utiliser double stirling_ans1; que int stirling_ans1;


Minor:
ne sait pas pourquoi code utilise des approximations grossières de pi et e. Peut-être:

#define PI 3.1415926535897932384626433832795 
#define e 2.7182818284590452353602874713527 

Ou mieux encore, les valeurs de forme d'un calcul unique:

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