Rekursive Berechnung der Exponentialfunktion in C
e(x)=1+ x/1! + x^2/2! + x^3/3! + ... + x^n/n! mit x^n/n! < Ɛ#include< stdio.h>
#include< stdlib.h>
unsigned long fact_recursive (unsigned short nombre)
{
if (number == 0)
return 1;
else
return number * fact_recursive(number - 1);
}
unsigned int puiss(long int x, int n)
{
if(n == 0)
Rückgabe 1;
if(n == 1)
return x;
int x2 = powers(x,n/2);
if(n%2 == 0)
Rückgabe x2*x2;
return x2 * x2 * x;
}
double calcul_formule(int x, int n, int epsilon)
{
doppelter Bruch = (doppelt)puiss(x,n) / (doppelt)fact_recursive(n);
if(n==0)
{
return 1;
}
else{
//Überprüfung des Zustands, wenn Potenzen(x,n)/n! > epsilon
if(fraction < epsilon)
return fraction+calcul_formule(x,n-1,epsilon);
else
return calcul_formule(x,n-1,epsilon);
}
}
int main(int argc, char *argv[])
{
int x,n,epsilon;
printf("Geben Sie den Wert von x ein: ");
scanf("%d",& x);
printf("Geben Sie den Wert von n ein: ");
scanf("%d",& n);
printf( Geben Sie den Wert von Epsilon ein: ");
scanf("%d",& Epsilon);
//Berechnung der Formel
printf("exp( %d ) = %f\n",x,calcul_formule(x,n,epsilon) );
system("pause");
}