Calculate the PI (π) of the circumference ratio, accurate to 10000 digits after the decimal point (C language)

Source: Internet
Author: User

Everyone knows that π = 3.1415926 ...... There are infinite digits. In history, many people have been calculating this number, which has always been regarded as a very complicated problem. Now that you have a computer, the problem is simple.
The computer can use the series to calculate a lot of high-precision values. For details about the series, see advanced mathematics. The following are famous π series:



Some of them are complicated to calculate. We can choose the third one, which is relatively simple and fast to converge.
Because the π value is calculated, and this formula is used to calculate π/2, we will deform it:
π = 2 + 2/3 + 2/3*2/5 + 2/3*2/5*3/7 +...

--------------------------------------------------------------------------------

For the series, we will first perform a simple test, and do not require accuracy for the moment:
Create a project using C ++ builder, put a memo1 and a button1 on the form, and write in The onclick event of button1:

Void _ fastcall tform1: button1click (tobject * sender)
{
Double X = 2, Z = 2;
Int A = 1, B = 3;
While (z> 1e-15)
{
Z = z * A/B;
X + = z;
A ++;
B + = 2;
}
Memo1-> text = ansistring (). sprintf ("Pi = %. 13f", X );
}

Display the execution result in memo1 by button1:

Pi = 1, 3.1415926535898

--------------------------------------------------------------------------------

This program is too simple, and the precision of double is very low, only the digits after the decimal point can be calculated.
 the above program to make it accurate to 1000 digits after the decimal point and then test it:
Put another button button2 on the form, and write it in The onclick event of the button:

Void _ fastcall tform1: button2click (tobject * sender)
{
Const arrsize = 1010, dispcnt = 1000; // defines the array size and number of digits
Char X [arrsize], Z [arrsize]; // X [0] X [1]. X [2] X [3] X [4]... X [ARRSIZE-1]
Int A = 1, B = 3, C, D, run = 1, CNT = 0;

Memset (x, 0, arrsize );
Memset (z, 0, arrsize );

X [1] = 2;
Z [1] = 2;

While (run & (++ CNT <200000000 ))
{
// Z * =;
D = 0;
For (INT I = ARRSIZE-1; I> 0; I --)
{
C = Z [I] * A + D;
Z [I] = C % 10;
D = C/10;
}
// Z/= B;
D = 0;
For (INT I = 0; I <arrsize; I ++)
{
C = Z [I] + D * 10;
Z [I] = C/B;
D = C % B;
}
// X + = z;
Run = 0;
For (INT I = ARRSIZE-1; I> 0; I --)
{
C = x [I] + Z [I];
X [I] = C % 10;
X [I-1] + = C/10;
Run | = Z [I];
}
A ++;
B + = 2;
}
Memo1-> text = ansistring (). sprintf ("% d times calculated/R/N", CNT );
Memo1-> text = memo1-> text + ansistring (). sprintf ("Pi = % d. /R/N ", X [0], X [1]);
For (INT I = 0; I <dispcnt; I ++)
{
If (I & (I % 100) = 0 ))
Memo1-> text = memo1-> text + "/R/N ";
Memo1-> text = memo1-> text + (INT) x [I + 2];
}
}

Run button2:

Pi = 03.
1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094
3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235
4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859
5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303
5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989

--------------------------------------------------------------------------------

Now, my mind is exhausted. Can I change the array size to calculate more digits? The answer is yes.
If you change the size and number of digits of the defined array:

Const arrsize = 10100, dispcnt = 10000; // defines the array size and number of digits

The accuracy of the execution result can reach 10000 bits:

Pi = 03.
1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094
3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235
4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859
5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303
5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
3809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151

... The space limit is omitted here. Let's leave it for you!

5020141020672358502007245225632651341055924019027421624843914035998953539459094407046912091409387001
2645600162374288021092764579310657922955249887275846101264836999892256959688159205600101655256375678

--------------------------------------------------------------------------------

Principle of improving accuracy:

The principle of the above program is to use an array to save the calculation result. Each item of the array is saved as one of the 10 hexadecimal numbers,
The decimal point is located between the first two integers in the array and the second number, and the rest are decimal places.

Using a computer to simulate four arithmetic operations to achieve high-precision data computing, I did not expect the most original method to be the highest accuracy.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.