3

Calculating PI can be done using the following formula:

π = 4 - 4/3 + 4/5 -4/7 +4/9 -4/11 ....

The more one goes further the sequence, the better accuracy one can get. My code seems correct, I'm adding the values to variable pi. However, once I print the variable pi I'm getting only zeros. What is wrong?

#include <stdio.h>

int main()
{

    long double pi =0.0;
    long double num= 4.0;
    long double denom= 1.0;
    long int loop;
    long int accuracy;

    accuracy= 400000;

    printf("Accuracy set at: %ld\n", accuracy);
    printf("term\t\t pi\n");

    for(loop=1;loop<=accuracy; loop++)
    {
        if(loop%2 != 0)
        {
            pi += num/denom;
        }
        else{
            pi-= num/denom;
        }
        printf("%ld\t\t%lf\n", loop, pi);
        denom +=2.0;
    }
    return 0;

}
crayzeewulf
  • 5,840
  • 1
  • 27
  • 30
  • Just curious - there is a 4 in everything. Why not take the 4 out? Is there a reason for keeping the 4 in each term of the expansion? – Martin James Oct 20 '15 at 00:26
  • It is printing only 0.000000 in the PI side of the table. It should be getting closer and closer to 3.1415 – Rodrigo Proença Oct 20 '15 at 02:56
  • @MartinJames, this is a mathematical formula given by the problem. If we take the 4 out we will not be getting closer to the real value of PI. – Rodrigo Proença Oct 20 '15 at 02:57
  • 1
    Almost identical question [here](http://stackoverflow.com/questions/32791411/c-long-double-and-printf-issue) and explanation [here](http://stackoverflow.com/a/7136886/1062948). – crayzeewulf Oct 20 '15 at 23:23

4 Answers4

4

My C compiler gives this message on your printf statement:

warning: format specifies type 'double' but the argument has type 'long double' [-Wformat]

Maybe if you use %Lf instead of %lf to print pi?

Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
  • Yup. [Here](http://ideone.com/E5mGJJ) is the result. This would not be an issue if you use C++-style streams (`std::cout` [for example](http://ideone.com/nGBs3B)). – crayzeewulf Oct 20 '15 at 00:17
  • 1
    Im using Dev C++ to compile it. I used it in Ideone.com and it worked just fine like you have shown here, but not really working in my IDE, should I use Visual Studio or some other compiler instead? – Rodrigo Proença Oct 20 '15 at 03:06
  • @RodrigoProença Can you try replacing the header `stdio.h` with `cstdio` while using Dev C++ and see if you get the correct results? – crayzeewulf Oct 20 '15 at 23:11
  • @RodrigoProença If using mingw-w64 you must also include `#define __USE_MINGW_ANSI_STDIO 1` when compiling, to get the correct behaviour. In its default state the compiler is bugged. If using the old mingw then you should upgrade to mingw-w64. – M.M Apr 15 '16 at 04:58
1

Instead of the if / else to handle alternating add and subtract:

for(loop=1;loop<=accuracy; loop++)
{
    pi += num/denom;
    printf("%d\t\t%Lf\n", loop, pi);
    denom += 2.0;
    num = -num;
}
rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

Get rid of the long from long double. I tested it out and it seems to be working

-1

You need to make a fitting else if() with it.

for (int loop=1; loop<=accuracy; loop++) {
    if(loop%2 != 0)
      {
        i++;
        if (i%2==0)
          pi += num/denom;
        else
          pi -= num/denom;
      }
    denom++;
}