### 2Use

#### 2.1Provided Continued Fractions

All of the continued fractions provided are sequences. If you want to see the terms of the simple continued fraction generated, then the standard use should be something like
 (for/list ((term (exp-cf 1/2)) (i (in-range 20))) term)
It is important to have a limiting sequence included in the for-clause because the provided continued fraction procedures of transcendental and algebraic functions tend to produce an infinite number of terms.
 procedure n : exact?
Creates a continued fraction representation of the argument.
 procedure cf : (listof (and/c number? exact?))
Interprets a list of terms as a continued fraction expansion of a rational, and gives that rational.
 procedure v : any/c
Determines whether the argument is a continued fraction.
 procedure
A continued fraction of the golden ratio.
 procedure
A continued fraction of pi.
 procedure n : exact?
A continued fraction of the natural exponential function.
 procedure n : exact?
A continued fraction of the natural logarithm function.
 procedure b : (and/c exact? integer? (>/c 1)) n : exact?
A continued fraction for the base-b logarithm function.
 procedure n : exact?
 procedure n : exact?
 procedure n : exact?
Standard trigonometric functions.
 procedure n : exact?
 procedure n : exact?
 procedure n : exact?
Standard hyperbolic functions.
 procedure(expt-cf base exponent) → continued-fraction? base : exact? exponent : exact?
Raising exact numbers to exact rational powers.
 procedure base : (and/c exact-integer? (>/c 1))
Champernowne’s constant for the given base. Champernowne’s constant is obtained by concatenating the base-b representation of ascending naturals. For instance, in base 10, the constant would be the irrational number 0.12345678910111213.... Champernowne’s constant has an irrationality measure of the base, which means that as the base increases the number becomes extremely well-approximated by rational numbers. In the terms of this library, the consume-limit may have to be increased considerably in order to guarantee the accuracy of the emitted terms. For instance, the 41st term of the continued fraction of the constant in base 10 has over 2000 digits. Trying to even display the 40th term will collide with this as the continued fraction tries to decide what term to settle on. The base 2 constant, by contrast, is only about as well-approximated as algebraic irrationals (e.g. those produced by expt-cf).

#### 2.2Arithmetic Procedures

 procedure v : (or/c exact-integer? continued-fraction?)
 procedure v : (or/c exact-integer? continued-fraction?)
 procedure v : (or/c exact-integer? continued-fraction?)
 procedure v : (or/c exact-integer? continued-fraction?)
Standard arithmetic procedures for continued fractions.

Example:
 > (for/list ((t (cf/ (pi-cf) (phi-cf) (expt-cf 2 1/2))) (i (in-range 20))) t)

'(1 2 1 2 7 6 3 1 36 2 36 1 4 2 1 1 3 7 1 3)

#### 2.3Controlling Behavior

There are two ways provided to control the behavior of continued fractions. The first is to limit the number of terms continued fractions are allowed to consume while they attempt to produce the next output term.

parameter

(consume-limit n)  void?
n : (and/c exact? positive? integer?)
 = 500
Every continued fraction has internal sequences it continues when it needs to generate the next term. In order to generate a term, the internal sequence may be continued many times, up to this limit. This parameter is the limit used. It is reset for every term the continued fraction generates. If the limit is reached then:
1. A message is sent to the current-error-port indicating the fact.

2. The current state is coerced to a rational and the terms of this rational become the rest of the sequence.

 procedure cf : continued-fraction?

parameter

(precision n)  void?
n : (and/c positive? number?)
 = (expt 2 30)
Ordinarily, continued fractions are allowed to produce as many terms as requested. But this behavior can be controlled with the combined use of precision-emit and precision. The precision parameter represents the denominator in the error bound of the approximation (see Precision), so a value of 100 means that the absolute value of the error between the continued fraction and the approximant yielded so far is less than 1/100. This value can be set to +inf.0, in which case the terms need to be stopped in some other way (e.g. through the use of (in-range 20) in a for-clause). This is equivalent to pulling terms from the continued fraction itself.

Examples:
 > (define ~pi (parameterize ((precision 1000)) (for/list ((t (precision-emit (pi-cf)))) t)))
> ~pi

'(3 7 15 1)

> (abs (/ (- pi (cf-terms->rational ~pi))))

3748629.0878493083

 > (parameterize ((precision 3748630)) (for/list ((t (precision-emit (pi-cf)))) t))

'(3 7 15 1 292 1)

 > (displayln (parameterize ((precision +inf.0)) (for/list ((t (precision-emit (pi-cf))) (i (in-range 40))) t)))

(3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 2 2 2 1 84 2 1 1 15 3 13 1 4 2 6 6 99 1 2 2 6 3 5)

 > (displayln (for/list ((t (pi-cf)) (i (in-range 40))) t))

(3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 2 2 2 1 84 2 1 1 15 3 13 1 4 2 6 6 99 1 2 2 6 3 5)

#### 2.4Arithmetic Caveats

There is no way, in general, to determine whether an infinite continued fraction is exactly a rational number; particularly whether it is zero. And there is no way to know whether some arithmetic expression is zero, so therefore there is no way to determine if we are dividing by zero. But continued fraction arithmetic is a little more lenient in that division by zero is "possible" in the degenerate sense that the empty continued fraction is infinity. (For a justification of this, please see "Exact Real Computer Arithmetic with Continued Fractions," Vuillemin (1988).) Thus, division by zero in this library will not produce errors but instead empty continued fractions.

Examples:
 > (for/list ((t (cf/ (exp-cf 1) (cf- (phi-cf) (phi-cf)))) (i (in-range 10))) t)
 consume limit reached consume limit reached

'()

 > (for/list ((t (cf/ (exp-cf 1) (cf- 2 2))) (i (in-range 10))) t)

consume limit reached

'()

 > (for/list ((t (cf/ (exp-cf 1) (rational->cf 0))) (i (in-range 10))) t)

consume limit reached

'()

 > (for/list ((t (cf/ (rational->cf 0) (rational->cf 0)))) t)

'()

Expressions which yield exact rationals from infinite continued fractions have their own problems. It would take an infinite number of terms to yield the rational but at any one time we’ve only ever taken a finite number of them.

Examples:
 > (let ((sqrt (λ(n) (expt-cf n 1/2)))) (for/list ((t (cf* (sqrt 2) (sqrt 2))) (i (in-range 10))) t))

consume limit reached

'(2)

 > (for/list ((t (cf- (expt-cf 2 1/2) (expt-cf 2 1/2))) (i (in-range 10))) t)

consume limit reached

'(0)

It would not matter how far we increased the consume limit as we could never increase it to infinity. In the case of the arithmetic of two continued fractions, if neither continued fraction has run out of terms by the time consume-limit is reached then it rounds the internal rational. Thus, in the calculation of (cf* (sqrt 2) (sqrt 2)) above the guess made by the library is '(2) rather than something like '(2 10000000000000000000000) or '(1 1 99999999999999999999999).

#### 2.5Emitting Terms in Specific Number Bases

 procedure v : continued-fraction? b : (and/c exact-integer? (>/c 1))
Emits terms as if it were a base-b expansion rather than a continued fraction. The integer part does not respect the limits of the base as there is otherwise no way to determine where it would end, so this is mostly interesting for the fractional parts of numbers. But see also representation-emit in continued-fractions/bases.

The base emitter does not emit terms as a usual continued fraction where only the first non-zero term is allowed to be negative since that would change the value of the integer-part; instead, all terms are negative.

Examples:
> (define phi (cf/ (cf+ 1 (expt-cf 5 1/2)) 2))
 > (for/list ((t (base-emit phi 10)) (i (in-range 20))) t)

'(1 6 1 8 0 3 3 9 8 8 7 4 9 8 9 4 8 4 8 2)

> (/ (+ 1 (sqrt 5)) 2)

1.618033988749895

 > (for/list ((t (base-emit (rational->cf 1/10) 2)) (i (in-range 20))) t)

'(0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0)

 > (for/list ((t (base-emit (rational->cf 2/3) 3))) t)

'(0 2)

 > (for/list ((t (base-emit (rational->cf 101/3) 3)) (i (in-range 20))) t)

'(33 2)

 > (for/list ((t (base-emit (cf- (phi-cf) (exp-cf 1)) 10)) (i (in-range 20))) t)

'(-1 -1 0 0 -2 -4 -7 -8 -3 -9 -7 0 -9 -1 -5 0 -3 -8 -7 -1)

procedure

s : sequence?
b :
 (and/c exact-integer? (>/c 1))
The counterpart of base-emit.

Examples:
 > (for/list ((t (pi-cf)) (i (in-range 10))) t)

'(3 7 15 1 292 1 1 1 2 1)

 > (for/list ((t (base-emit (pi-cf) 10)) (i (in-range 10))) t)

'(3 1 4 1 5 9 2 6 5 3)

 > (for/list ((t (base-consume (base-emit (pi-cf) 10) 10)) (i (in-range 10))) t)

'(3 7 15 1 292 1 1 1 2 1)