I have to implement a simple infinite sum which estimates the value of PI. The more iterations of the infinite sum, the more accurate the value of PI. Using the equation PI=4x(1-1/3+1/5-1/7+1/9-1/11+1/13-....)
.The details of the program are as follows:

1. The program must have a small menu for the purpose of user
interaction. The menu will contain the following choices:

1. Exit the program
2. Set how many digits past the decimal point are to be calculated
3. Calculate the value of Pi
(this part works for me)

2. I have to figure out how to determine when a digit is valid. (probably
by converting the floating point value of PI to a string and
checking for how many iterations the value of a digit is
unchanged.) Any valid algorithm to determine a valid digit is fine;


here is my code so far

Dani AI

Generated

A few practical points tied to what you posted.

Your menu/interaction approach works, but the current use of a fixed PI value and plain double will never let the program "calculate" pi beyond the usual 15 significant digits. The Leibniz series you mentioned also converges extremely slowly: to guarantee D correct decimal places from that series you need on the order of 4 * 10^D terms (so ~400,000 steps for 5 decimals, ~4,000,000 for 6). That makes Leibniz fine for learning, but impractical for many digits.

A reliable way to decide when a digit is valid:

  • If you stick with an alternating series whose terms decrease in magnitude (Leibniz fits), use the alternating-series remainder bound: the error is at most the magnitude of the first omitted term. Solve that inequality for N to know how many terms are required to guarantee D digits.
  • A safer, general approach is to compute pi with arbitrary precision (digits + a small guard), and compare scaled integer truncations rather than formatted strings. Compute two high-precision approximations (or compute once with extra precision) and check that floor(pi * 10^D) is the same across successive approximations. Require equality for a couple of checks (or use guard digits) to avoid carry/rounding surprises.

Small pseudocode (conceptual):

prec = digits + guard
scale = 10^digits
pi_prev = compute_pi(prec)
loop:
  pi_cur = compute_pi(prec)  // more terms or same with different method
  if floor(pi_prev*scale) == floor(pi_cur*scale) for 2 iterations:
    digits are stable
  pi_prev = pi_cur

For real work, use an arbitrary-precision library (Boost.Multiprecision, GMP/MPFR) and a fast series (Machin-like or the Chudnovsky algorithm) instead of Leibniz. See the Boost multiprecision docs and the Chudnovsky algorithm for practical implementations and performance expectations:

Notes: store the "number of digits" as an integer (not a double), and avoid relying on formatted strings alone — they can hide unstable carries from lower digits.

I have to implement a simple infinite sum which estimates the value of PI. The more iterations of the infinite sum, the more accurate the value of PI. Using the equation PI=4x(1-1/3+1/5-1/7+1/9-1/11+1/13-....)
.The details of the program are as follows:

1. The program must have a small menu for the purpose of user
interaction. The menu will contain the following choices:

1. Exit the program
2. Set how many digits past the decimal point are to be calculated
3. Calculate the value of Pi
(this part works for me)

2. I have to figure out how to determine when a digit is valid. (probably
by converting the floating point value of PI to a string and
checking for how many iterations the value of a digit is
unchanged.) Any valid algorithm to determine a valid digit is fine;


here is my code so far

if anyone could help me it would really save me

if anyone could help me it would really save me

i am using codeblocks and the gcc compiler

where is the code...??

#include <stdio.h>
#include <math.h>
#include <float.h>


//Define PI
#define PI 3.14159265

int main()
{
    //Variable declarations
    double a=DBL_MAX, b=DBL_MAX, c=DBL_MAX;

    //Menu control variable set to an initial value
    int inpUserChoice=0;

    //Main superloop structure
    while(inpUserChoice!=1)
    {


        if(b!=DBL_MAX)
            printf("\n# of digits = %.0lf", b);
        else
            printf("\n#of digits = UNDEFINED");

        //output menu choices
        printf("\n\n 1. Exit Program");
        printf("\n 2. Set how many digits past the decimal point are to be calculated");
        printf("\n 3. Calculate the value of Pi");


        //Prompt the user for input
        printf("\n\nEnter Selection: ");
        scanf("%d", &inpUserChoice);

        //Branch structure to handle selection
        if (inpUserChoice==3)

        {

            //Prompt the user for input
            printf("\n\nthe value of pi to %.0lf decimal spots is: %lf", b, PI);
            scanf("%lf", &a);
        }
        else if (inpUserChoice==2)
        {
            //Prompt the user for input
            printf("\n\nEnter the number of digits to be calculated: ");
            scanf("%lf", &b);
        }

        else if (inpUserChoice==1){
            printf("Exiting program...");
            return 0;}
        else
            printf("\n\nINVALID SELECTION %d is not an option in this program\n\n", inpUserChoice);    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.