Previous Up Next

6.2.7  Continued fraction representation of a real

Any real number a can be written as a continued fraction:

a= a0+
1

a1+
1

a2+
1

a3 + ⋯
 

, which is often abbreviated [a0;a1,a2,a3,…]. The dfc command writes a real number as a continued fraction.

Remarks.

Examples

dfc(sqrt(2),5)
     

1,2,
2

          
dfc(evalf(sqrt(2)),1e-9)

or:

dfc(sqrt(2),1e-9)
     

1,2,2,2,2,2,2,2,2,2,2,2,2
          
convert(sqrt(2),confrac,'dev')

With epsilon=1e−9 in the CAS configuration, we obtain:

     

1,2,2,2,2,2,2,2,2,2,2,2,2
          

and [1,2,2,2,2,2,2,2,2,2,2,2,2] is stored in dev.

dfc(9976/6961,5)
     



1,2,3,4,5,
43
7



          

Input to verify:

1+1/(2+1/(3+1/(4+1/(5+7/43))))
     
9976
6961
          
convert(9976/6961,confrac,'l')

With epsilon=1e−9 in the CAS configuration, we obtain:

     

1,2,3,4,5,6,7
          

and [1,2,3,4,5,6,7] is stored in l.

dfc(pi,5)
     



3,7,15,1,292,
−113 π +355
33102 π −103993



          
dfc(evalf(pi),5)

If floats are hardware floats, e.g. for Digits=12, we obtain:

     

3,7,15,1,292,1.57581843574
          
dfc(evalf(pi),1e-9)

or:

dfc(pi,1e-9)

or, with epsilon=1e−9 in the CAS configuration:

convert(pi,confrac,'ll')
     

3,7,15,1,292
          

and [3,7,15,1,292] is stored in ll.


Previous Up Next