Problems with pi approximation algorithm in C

Asked

Viewed 326 times

1

Good evening, I’m having the following problem, I’m developing an algorithm to calculate a pi approximation.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <locale.h>

int mmc(int num1, int num2) {

    int mmc, aux, c;
    if(num2==1)mmc=num1;
    else{
        for (c = 2; c <= num2; c++) {
            aux = num1 * c;
            if ((aux % num2) == 0) {
                mmc = aux;
                c= num2 + 1;
            }
        }
    }
    return mmc;
}

double R1(int num){
    int cont=1;
    double pi=1.0, dAtual=1.0,dAnt,dCom=1.0,nume=1.0;
    for(;cont<=num;cont++){

        dAnt=dCom;
        dAtual=dAtual+2.0;
        dCom=mmc(dAtual,dAnt);
        if(cont%2==0) nume=nume*(dCom/dAnt)+(dCom/dAtual);
        else nume=nume*(dCom/dAnt)-(dCom/dAtual);
        pi=nume/dCom;


    }

    pi=pi*4;
    return pi;
}

double R2(int num){
    int cont=1;
    double pi=1.0, dAtual=1.0,dAnt,dCom=1.0,nume=1.0;
    for(;cont<=num;cont++){

        dAnt=dCom;
        dAtual=pow((cont+1.0),2);
        dCom=mmc(dAnt,dAtual);
        nume=nume*(dCom/dAnt)+(dCom/dAtual);
        pi=nume/dCom;


    }

    pi=sqrt((pi*6));

    return pi;
}

int main(){
    setlocale(LC_ALL,"Portuguese");
    int n;

    printf("Digite um numero positivo: ");
    scanf("%d",&n);



    printf("\nA aproximação de pi com %d termos é:\n\n",n);
    printf("R1= %.15lf\n\n",R1(n));

    printf("R2= %.15lf\n\n",R2(n));

system("pause");
return (0);
}

The problem is in function R2, which from the 12th term can no longer calculate and returns -1,#IND0000, I thought I could have exceeded the double limit value, and tried to change to long double, but this way only returns 0. What could be the problem?

The formula R2 was based on is this: inserir a descrição da imagem aqui

2 answers

0

I fixed both R1 and R2 functions and found that the mmc function in this case was useless, the problem was much simpler than I thought. Follow the code in a way that works:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <locale.h>

double R1(int n){
    int cont=2;
    double pi=1.0,i=3.0;
    if(n!=1){
        for(;cont<=n;cont++){

            if(cont%2==0) pi=pi-1.0/i;
            else pi=pi+1.0/i;
            i=i+2.0;

        }
    }

    return (pi*4.0);
}

double R2(int n){
    int cont=2;
    double pi=1.0,i=2.0;
    if(n!=1){
        for(;cont<=n;cont++){

            pi=pi+1.0/(i*i);
            i=i+1.0;

        }
    }

    return (sqrt(pi*6.0));
}

int main(){
    setlocale(LC_ALL,"Portuguese");
    int n;

    printf("Digite um numero positivo: ");
    scanf("%d",&n);



    printf("\nA aproximação de pi com %d termos é:\n\n",n);
    printf("R1= %.15lf\n\n",R1(n));

    printf("R2= %.15lf\n\n",R2(n));

system("pause");
return (0);
}

0


#IND means an indeterminate form.

What you have there is known as 'Not a number' or Nan for short.

To quote Wikipedia, the generation of this is made by:

  • Operations with one Nan as at least one operand.
  • Divisions 0/0 and /
  • The multiplications 0 and 0
  • The additions + ( ), ( ) + and equivalent subtractions
  • The square root of a negative number.
  • The logarithm of a negative number
  • The reverse sine or cosine of a number less than -1 or greater than +1.

You’re doing at least one of those things.

Edit

Only by complementing the answer, with the code that the Cleber posed, the problem was in the function mmc.

The code would look like this:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <locale.h>

double R1(int n){
    int cont=2;
    double pi=1.0,i=3.0;
    if(n!=1){
        for(;cont<=n;cont++){

            if(cont%2==0) pi=pi-1.0/i;
            else pi=pi+1.0/i;
            i=i+2.0;

        }
    }

    return (pi*4.0);
}

double R2(int n){
    int cont=2;
    double pi=1.0,i=2.0;
    if(n!=1){
        for(;cont<=n;cont++){

            pi=pi+1.0/(i*i);
            i=i+1.0;

        }
    }

    return (sqrt(pi*6.0));
}

int main(){
    setlocale(LC_ALL,"Portuguese");
    int n;

    printf("Digite um numero positivo: ");
    scanf("%d",&n);



    printf("\nA aproximação de pi com %d termos é:\n\n",n);
    printf("R1= %.15lf\n\n",R1(n));

    printf("R2= %.15lf\n\n",R2(n));

system("pause");
return (0);
}
  • 1

    Actually, I found out now that the mmc function returns me at some point a value like this, I switched it for a simple multiplication Dant*dAtual in R2, but at 98th term it stops working again.

  • Don’t forget to mark as resolved

Browser other questions tagged

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