1

I wrote very simple program in C (PI computation) and I got result like this: 3.1406174152499528235728265546100601568468846380710601806640625

Result doesn't look like proper PI then I found code in Python on internet I ran it and got something like that: 3.1415926535897932384626433832795028841971693993751058209749 - Proper PI.

Then I implemented Python algorithm in C with exactly the same way and I got result like that: 3.1406174152499528235728265546100601568468846380710601806640625

Why results form Python and C from the same algorithm are so different and how to fix C code to get right result ??

Python code:

from decimal import*
precision = 100
getcontext().prec = 100
a = 1
b = Decimal(1) / Decimal(2).sqrt()
t = Decimal(1) / Decimal(4)
p = 1
pi = 0
for x in range(0, precision):
 nextA = (a + b) / 2
 nextB = (a * b).sqrt()
 nextT = t - p * ((a - nextA) ** 2)
 nextP = 2 * p
 pi = ((a + b) ** 2) / (4 * t)
 a,b,t,p = nextA,nextB,nextT,nextP
print pi

C code:

long double a = 1;
long double b = 1/sqrt(2);
long double t = 0.25;
long double p = 1;
long double an, bn, tn, pn, myPI;
long int x;
for(x = 0; x < 100; x++)
{
 an = (a + b) / 2;
 bn = sqrt(a * b);
 tn = t - p * ((a - an) * (a - an));
 pn = 2 * p;
 myPI = ((a + b) * (a + b)) / (4 * t);
 a = an; b = bn; t = tn; p = tn;
 }
printf("%.61Lf\n", myPI);
return 0;
asked Jul 18, 2011 at 18:50
3
  • 3
    You do not have 100 precision in C (without an external library like GMP). long double is about 80 binary digits precision. Commented Jul 18, 2011 at 18:53
  • Actually, neither of these are 'proper' PI; PI is not a rational number, and thus cannot be expressed as a finite fraction, no matter the base. Commented Jul 18, 2011 at 18:58
  • 1
    Here proper means proper up to the number of decimal positions provided. Commented Jul 18, 2011 at 19:02

3 Answers 3

8

The problem is a typo in your code, p = tn; should be p = pn;.

Note: for long doubles use sqrtl.

Update: if you print out the approximation for pi after each iteration it doesn't get any better after the fifth iteration and only the first 20 digits are correct. for more precision you need better (not builtin) data types.

answered Jul 18, 2011 at 19:02
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't notice that bug.
6

You are asking for more precision than your C floating point variables can provide. You need to use arbitrary precision arithmetic rather than fixed precision floating point. Or at the very least use fixed precision arithmetic with a precision value greater than your desired calculation precision. The Python code sets the precision to 100 to achieve this.


Well, @yi_H also found a bug in your code but even when you fix that you won't get the same precision from your C code as from your Python code for the reasons above.

answered Jul 18, 2011 at 18:52

Comments

0

The reason is that floating-point numbers (typically 80 bits total) are the most precise numeric data type C has to offer out-of-the-box, and for most real-world computations, that's more than enough (OTOH, a float, no matter how large, cannot exactly represent 0.85 for example, but that aside).

Python has a built-in big-integer type which allows it to represent arbitrarily large integers (and, piggy-backing on this implementation, arbitrarily precise rational numbers). By using these, approximations of PI can be calculated that are far more precise than anything a float can hold.

If you want even more precision, you'll need to find an algorithm that calculates digits one by one, and have it spit them out while calculating.

answered Jul 18, 2011 at 19:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.