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:

                                                                                                           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

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.)