2

I have this assignment

The value of Pi can be determined by the following product

         2 * 2   4 * 4   6 * 6               N * N
Pi = 2 * ----- * ----- * ----- * ... * -----------------
         1 * 3   3 * 5   5 * 7         (N - 1) * (N + 1)

Write a C program that calculates the approximated value of Pi as long as the general term is greater than 1 + 10-9.

To solve it, I wrote this code:

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

int main() {
  double pi;
  const double End =
      1.0 + (1.0 / (10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0));
  double N = 2.0;

  pi = 2.0 * ((N * N) / ((N - 1.0) * (N + 1.0)));

  while (pi > End) {

    N += 2.0;
    pi *= ((N * N) / ((N - 1.0) * (N + 1.0)));
  }
  printf("%lf", pi);

  return 0;
}

I can't really understand how things work. I managed to use only double variables and added .0 to all the number literals, but the program is stuck and doesn't give any value when I launch it.

Why?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Jack_Kai
  • 19
  • 5
  • Try printing the value of `pie` in each iteration of the loop. – dbush Jan 12 '21 at 18:09
  • 1
    Just so you know, you can also write 10^-9 as 10e-9, rather than a long line – rohitt Jan 12 '21 at 18:12
  • i think i know what's the problem now thanks to dbush , when i printf("%lf",pie); in the while loop , it turned out to be an infinite loop which indicates that there is a math problem since pie will always be greater that 1+10e-9. I wonder now if i git the objective of the question wrong then – Jack_Kai Jan 12 '21 at 18:15
  • I think you are looking for the difference between your formula and the real value to be less than End. Dont' forget to take absolute value of difference. – stark Jan 12 '21 at 18:17
  • https://imgur.com/a/5Sb7dCP , a photo of the exercise question , how should i solve it then? – Jack_Kai Jan 12 '21 at 18:20
  • The "general term" is the value of each added iteration. Once the value of the iteration to be added is less than the limit, you should terminate the progression. – Mark Benningfield Jan 12 '21 at 18:28
  • @MarkBenningfield Not added, multiplied. – Yunnosch Jan 13 '21 at 15:48

2 Answers2

0

A simple solution would be to keep track of the old value, and thus replace the loop to the following (needs <math.h>)

double pi_old = pi + 1e9; // initialize to large value
while (fabs(pi-pi_old) > End){
    pi_old = pi; // store this

    // then compute next
    N += 2.0;
    pi *= ((N * N) / ((N - 1.0) * (N + 1.0)));
}

Also, As I've commented,

const double End =
      1.0 + (1.0 / (10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0));

is equivalent to

const double End = 1.0 + ( 1.0e-9 );  // 1.0e-9 = 1.0*10^(-9)
rohitt
  • 1,124
  • 6
  • 18
  • `End` as correctly calculated by OP, is slightly larger than 1. `while (fabs(pi-pi_old) > End)` could end with Pi being 2.14 or 4.14. That is unlikely what the teacher expects. The required condition is targeting a factor, not an increment. – Yunnosch Jan 13 '21 at 15:47
0

This condition while (pi > End) does not match the requirements.
It will loop while the total product is larger than roughly 1, which for any approximiation of Pi will be an endless loop.
The decription refers to only the last part (N*N)/( (N-1) * (N+1) ) being larger than 1 + a little.

So the solution is to calculate that last part separately and use only it in the loop condition.

(I intentionally do not provide more details or code, because of
How do I ask and answer homework questions?
)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54