# Pi - Hm, somethign is not right here.

• 09-11-2008
Pi - Hm, somethign is not right here.
ok, here is my formula

PHP Code:

``` [CODE]/* Pi loops, we how many times??? */while ( cycles < loops ) { // While x is less than 10     cycles += 1;             /* 1 loop started. */    pi += (plus * (1 / ((2 * cycles) + 1)));    plus = (0 - plus);    /* tempPi = ((1 - pi) * 4); */    /* system("cls"); */    /* progress = ((cycles / loops) * 100) */;    printf("%.0f / %.0f\n",cycles,loops);}/* Done, end of loading */system("cls");/* Calculate pi (1)*/pi = (4 * (1 - pi));[/CODE]  ```
I am using this formula

---

I have several problems.

The main one
1 - Can I increase the number of decimal places - I am using float ( I get 3.141637086828610000000000000000000000000000000000 ?

2 - Why does it not give me the answer I expect - (I expect 3.14159265358...) (I tried with 2 million cycles).
- probably a bad wording, what is meant here is a question about how c++ works. Is there a limit to the decimals place during a calculation?

3 - math based, is there a better formula that i can use? I know there are other forumlas around but this one seems to be the one they recommend for programming.

---

This is not homework as such, I am not wanting someone to re-write it for me with a better formula and code as I am trying to do this to learn. Because it is not homework, I am willing to change to another language if it is free, similar syntax and is not too hard to learn.

---

Thanks

Anthony
• 09-11-2008
CornedBee
The answers to all three lie in the way floating point numbers work. I suggest looking them up in Wikipedia.

And yes, there are better ways to get PI, since the algorithm you have suffers greatly from the inaccuracy problem.

For example, many compilers have the constant M_PI in <math.h>. Just use that. Failing that, define PI as your own constant. Look the value up on some website.

Simply put, there's no good reason to ever compute PI yourself. It's a fundamental geometrical constant. It's not ever going to change as long as we perceive the world as an Euclidean, 3-dimensional space.
• 09-11-2008
Elysia
I believe it to be due to floating point inconsistencies (a floating point isn't accurately stored).
If you need better and/or more precision, try using a double or long double.
• 09-11-2008
CornedBee
Oh, and you could get integer math issues in this expression:
Code:

`(1 / ((2 * cycles) + 1))`
If cycles is an integer type, this will always evaluate to 0.

Edit: I see it is not. Never mind.
• 09-11-2008
I am calculating pi because I am trying to learn c++. And because I want to see the formulas work.
• 09-12-2008
ok, I have edited the code a bit myself now.

PHP Code:

``` [CODE]#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){double pi,a,b,c,x,a1,b1,c1,x1;float loops,cycles;/* Well, done no clyles yet. */cycles = 0;/* Title */printf("Pi\nBy Anthony\n");/* Get the number of loops */printf("Enter number of loops: ");scanf("%f", &loops);/* We are doing something... */printf("Calculating Pi...\nPlease Wait");/* Now lets set the first values. */a = 1;b = (1/pow(2,0.5));c = 0.25;x = 1;/* And then set last values to that... */a1 = a;b1 = b;c1 = c;x1 = x;/* Pi loops, we do it how how many times??? */while ( cycles < loops ) {    cycles += 1;             /* 1 loop started. */    /*start the calculations */    a = ((a1 + b1) / 2);    b = pow((a1 * b1),0.5);    c = c1 - x1 * pow((a - a1),2);    x = (2 * x1);    /* Set a1,b1,c1 and x1 ... */    a1 = a;    b1 = b;    c1 = c;    x1 = x;        /* show progress */    printf("%.0f / %.0f\n",cycles,loops);}/* Done, end of loading */system("cls");/* Calculate pi */pi = (pow((a + b),2) / (4 * c));/* Output */printf("Done. Pi is %.25f\n",pi);system("pause");}[/CODE]  ```
My question is, how can I get more than 15 decimals places?
• 09-12-2008
CornedBee
You could use a library like GMP that gives you arbitrary-precision numbers.
http://gmplib.org/pi-with-gmp.html
• 09-12-2008
anon
There should also be algorithms to compute Nth digit of pi, so you wouldn't need anything more than built-in types.
• 09-12-2008
master5001
Calculating digits of pi hardly seems like a logical choice of programs to write in order to learn a language. You should be aware of the computational complexity of many of the algorithms used to calculate digits of pi (at least the ones which calculate more than one digit per pass). Why not just write a calculator or something... If you were learning F or something, I could understand this being a logical program to write. I guess I am not here to judge.