Function calculates to a certain extent and to

Asked

Viewed 102 times

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 to s 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

  • That may be it. Thank you for the reply.

2 answers

3

In function calcSoma() the value you add to s 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

Value added to s with

i = 0; // numerador/fatorial(valFat) ==  2/ 3!  == 0.333...
i = 1; // numerador/fatorial(valFat) ==  4/ 5!  == 0.0333...
i = 2; //                                6/ 7!  == 0.00119...
i = 3; //                                8/ 9!  == 0.0000220...
i = 4; //                               10/ 11! == 0.000000250...
i = 5; //                               12/ 13! == 0.00000000192...

2

There is a number limit that can be represented by the numeric types, this formula is working with too large numbers. You need to create or use something ready that can handle such large values. What is not an easy thing to do. My suggestion is to use GMP.

Changing the formatting to present the number in scientific notation help better see the number (in this case not much).

#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 %e", 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;
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.