3
I have a variable double
making an account but it reaches a limit [in my case 1.367879] and does not increase more.
The code is running normally, the problem is the result that is not more than 1.367879
#include <stdio.h>
#include <stdlib.h>
double calcSoma(int n);
long int fatorial(int n);
int main () {
int termos;
do {
printf("Digite a qtd de termos (>= 5): ");
scanf("%d", &termos);
}while(termos < 5);
printf("\nO somatorio para %d termos eh %f", termos, calcSoma(termos));
}
double calcSoma (int n){
double s = 1;
int i, numerador = 0, valFat = 1;
for (i = 0; i < n; i ++) {
numerador = numerador + 2;
valFat = valFat + 2; //numeros impares
s = s + (double)numerador/fatorial(valFat);
}
return s;
}
long int fatorial(int n){
long int fat = 0;
if (n == 0) {
return 1;
}else {
fat = n * (fatorial(n-1));
}
return fat;
}
In function
calcSoma()
the value you add tos
becomes smaller and smaller much quick.numerador / fatorial(valFat)
is a number increasingly close to zero.1.367879 + 0.0000000000000....0006576423
never leaves 1.367879– pmg
That may be it. Thank you for the reply.
– Beto Silva