|
|
CS 3723
Programming Languages
Fall 2014 |
H11: Arbitrary-Precision
|
Arbitrary-Precision:
See Arbitrary-Precision Float
for Python's float extension, which I'm using below.
As we take increasing values for the depth of the fraction
evaluated, the
fraction programs will calculate fractions ever closer to π,
even with millions of digits in the denominator. First we show the
calculation done with constant 40 rather than 20
(taking the continued fraction to a depth of 40). The resulting
fraction is below. Notice that this fraction is exactly the value
of the continued fraction, truncated to level 40.
It is only approximately close to π. (Of course no finite fraction
can be exactly equal to π.)2258601801834136133882621891503456256 / 718935282476328405580165727291362725
We must have the arbitrary precision package to convert the longer
fraction to floats without losing precision.
The fraction above works out to be approximately equal
to the first line below, with the same number of digits of π shown
below it. The digits not equal to those in π are shown
in red below. 1 2 3 4 5 6 7
1234567890123456789012345678901234567890123456789012345678901234567890
ans = 3.1415926535897932384626433832797151980590967017069906067631916591498L0
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078L0
The result is accurate to 31 significant digits.
Depth 100:
Here the same Python program is run with a depth of 100 in the continued
fraction. The resulting fraction close to π is below:
|
230472573934774707842404216444096156856954356127677523896716863794721544322478959628646350848/
73361698777663417823509011957888573950952305745241626687452984196759740688178926126151382145
Here are the results of converting to a very high precision
floating point number (π correct to 76 digits):
1
1 2 3 4 5 6 7 8 9 0
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
ans = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862338293517098830162613758
5402952648126073337044009636826949777700233285141582L0
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
821480865132823066470938446095505822317253594081284811L0
|
Depth 1000:
Now a depth of 1000. The Python program took 30 seconds on elk01.
|
1
1 2 3 4 5 6 7 8 9 0
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5308018752133918538236794537857536507630436443866558728604502666099586341915186057352803174091679300
7163468467065929219909935712878365339495262087022819683092172398473643954523688852212839032132584304
5511236591425940324052312678517996718361480616621853445168802454252355851337614294350167743851798306
6747955075637603498492129348825285229343781851927290438796531067576629762337224727159633180257468618
0670277510153384791419155013388867170991087265814120662437973736874838845645662683251659178006711202
8647892149442138970656867033607630286852692805896242243024860921253002399715914776015469527414841362
8426542350276737381879707349950119620618382272388039907377006470227213576545614298299550930551751424
2377304272517673740706038562863996070864369161834936626394481609467996932496763439609783975055250432
7005883115083095808792886200697644382099299087815941336909167730487093151377522137001699023100097658
2583999518335280015918951105997652506968840532671244003180544 /
1689594844853174197629196396604025786216021789742738315464092671664592014946771163309896588054349813
2423790296540988963865147943453450015440838364297468343269118583947875463356754940804828200183075950
9573252392091267300515640795184642357015582413383546318404452833377497615720929660761956284368768784
4608719026550469131760555292909572429288577915761589566143292645256903792175017764754128871519994905
2312859693565275794228202057263210507301797740400868690730963204890362942932418463629902350537001707
8909977292364488930445552120557268656859882129310882085309322137641911225670333125040379839680227547
8271427860482668659824506350901183249790765048984016632721867479113976179650934993998778147982918989
0292364672009223852325936271476337797835244204713546059836481143445819422089495422298175941248917971
9561994512628574685233746961026312090773127184937220489558588486180383379361220844558636345222912439
5798973564880823762850002665147620646317697429562249385838125
Here are the results of converting to a
floating point number (π correct to 765 digits):
1
1 2 3 4 5 6 7 8 9 0
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
ans = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094
3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235
4201995611212902196086403441815981362977477130996051870721135000024923532103833515596819369592183200
7716100949428795120349135626122923973072042382801152328325505415228463144462608806828426902155840730
26267L0
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094
3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235
4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859
5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303
59826L0
( Revision date: 2014-11-20.
Please use ISO 8601,
the International Standard Date and Time Notation.)