Thread: Problem printing a PI

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Problem printing a PI

    First I must say that I'm new to C programming, a second sorry on my bad english!

    My problem is that I try to print more then 1 milion didgits of PI. I'm using Gauss-Legendre algorithm that is used in program Super_pi. I was using Google for some time now, and only that I could find is that I need to use some non standard library! Any help is useful! Code is attached.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(){
    	
    int i;
    float a = 1;
    float b = a/sqrt(2);
    float t = a/4;
    float x = a;
    float y;
    float p;
    for (i = 1; i <= 19; i++)
    	{
    		y = a;
    		a = (a+b)/2;
    		b = sqrt(b*y);
    		t = t-x*(a-y)*(a-y);
    		x = 2*x;
    	}
    
    p = (a+b)*(a+b)/(4*t);
    printf("%.1000000f",p);
    
    getch();
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly is your problem? I haven't bothered memorizing a million digits of PI, so you'll want to actually tell us what's wrong. You'll also be interested to know that floating point numbers are not accurate, so you'll likely have problems there too.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Problem is that first 16 numbers have some value 0-9 then the rest are zeros.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Maybe if you used discriptive variable names, I would be able to figure out what your doing. But as quzah said, the is no built in type to store 1 million digits of pi. I'd suggest that you simply print out each diget as you find it, but I don't know if that would work with your algorithm
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Quote Originally Posted by King Mir
    the is no built in type to store 1 million digits of pi.
    That is my problem, can this be solved in any way?!

    Quote Originally Posted by King Mir
    Maybe if you used discriptive variable names
    I used one from algoritham, its easyer for me! Here is original algoritham!

    Gauss-Legendre algorithm

    1. Initial value setting;

    a=1
    b=1/sqrt(2)
    t=1/4
    x=1

    2. Repeat the following statements until the difference of a and b is within the desired accuracy;

    y=a
    a=(a+b)/2
    b=sqrt(b*y)
    t=t-x*(y-a)^2
    x=2*x

    3. Pi is approximated with a, b and t as;

    PI=(a+b)^2/(4*t)

    The algorithm has second order convergent nature. Then if you want to calculate up to n digits, iteration count of the order log2 n is sufficient. E.g. 19 times for 1 million decimal digits, 31 times for 3.2 billon decimal digits.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  2. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  3. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 06:57 PM
  4. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 02:09 PM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM