Everyone knows π = 3.1415926 ... Infinity, many people in history are calculated, and they always think is a very complex problem. Now with a computer, this problem is simple. Computers can use the grade to calculate a lot of high-precision values. Please refer to "Higher Mathematics" on the problem of levels. The following is a relatively famous level of π:
Some of them are complicated, we can choose the third, relatively simple, and converge. Because the π value is calculated, this formula is calculated, we deform it: π = 2 2/3 2/3 * 2/5 2/3 * 2/5 * 3/7 .. .
For the level, let's make a simple test first, temporarily do not require the accuracy: create a new project with C Builder, put a MEMO1 and a Button1 on Form, write on the button 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);}
Press Button1 in MEMO1 to display the execution result: pi = 3.1415926535898
This program is too simple, and Double has a low precision, only 10 after the decimal point. Transform the above program, let it accurate to 1000 bits behind the decimal point: On the Form, put a button button 2, on this button on the onclick event:
Void __fastcall tform1 :: button2click (TOBJECT * Sender) {const arrsize = 1010, Dispcnt = 1000; // Defines an array size, display bit 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 * = a; 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
{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 ("calculated% D times / R / N", CNT); Memo1-> text = MEMO1-> text Ansistring (). Sprintf ("Pi = % D% d. / r / n ", x [0], x [1]); for (INT i = 0; i {= {= {= {=========================================================================================================== = MEMO1-> Text "/ r / n"; memo1-> text = MEMO1-> text (int) x [i 2];}}
Perform results by button2:
Pi = 03.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164 201989
This psychology has, is it possible to change the number of digits? The answer is yes. If the array size is defined and to display digits: const ARRSIZE = 10100, DISPCNT = 10000; // define the array size, the execution result display digit accuracy up to 10,000: Pi = 03.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042 875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151 ... Due to space limitations, here is omitted, or left to yourself to count it! 50201410206723585020072452256326513410559240190274216248439140359989535394590944070469120914093870012645600162374288021092764579310657922955249887275846101264836999892256959688159205600101655256375678 improve the accuracy of the principle: the principle of the above procedure using an array of the saved results, wherein each array holding a decimal number, the decimal number of the array is positioned between the first and the second number, i.e., The two-bit integer in front of the decimal point, and the rest are small digits. Using the computer to simulate the sum of four computational description methods to achieve high-precision data calculations, the most precise method did not expect the most accurate precision.