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

- dfc takes one mandatory argument and one optional argument:
  - a, a real number.
  - Optionally, n an integer or epsilon, a positive real number.
- dfc(a) returns the list of the continued fraction representation of a with precision epsilon, which is given by Section 2.5.7, item 2.5.7.
- dfc(a,epsilon) returns the list of the continued fraction representation which approximates a or evalf(a) with the specified precision epsilon.
- dfc(a,n) returns the list of the continued fraction representation of a of order n.

##### Remarks.

- The convert command with the option confrac (see Section 10.1.10) has a similar functionality: in that case the value of epsilon is the value defined in the CAS configuration and the answer may be stored in an optional third argument.
- If the last element of the result is a list, the representation is ultimately periodic, and the last element is the period. It means that the real is a root of an equation of order 2 with integer coefficients. So If dfc(a)=[a0,a1,a2,[b0,b1]] then:

  a=a0+

  1

  a1+

  1

  a2+

  1

  b0+

  1

  b1+

  1
  b0+⋯

  .

- If the last element of the result is not an integer, it represents a remainder r. For example, if dfc(a) is equal to [a0,a1,a2,r], then:

  a=a0+

  1

  a1+

  1

  a2+

  1
  r

  .

  Be aware that this remainder has lost most of its accuracy.

#### Examples

dfc(sqrt(2),5)

| ⎡ ⎣ | 1,2, | ⎡ ⎣ | 2 | ⎤ ⎦ | ⎤ ⎦

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

or:

dfc(sqrt(2),1 e-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),1 e-9)

or:

dfc(pi,1 e-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.