On this page:
3.1 Provided Continued Fractions
rational->cf
cf-terms->rational
continued-fraction?
phi-cf
pi-cf
exp-cf
ln-cf
log-cf
sine-cf
cosine-cf
tangent-cf
hyperbolic-sine-cf
hyperbolic-cosine-cf
hyperbolic-tangent-cf
arctangent-cf
arcsine-cf
arccosine-cf
expt-cf
sqrt-cf
in-terms
in-convergents
champernowne-cf
3.2 Arithmetic Procedures
cf+
cf-
cf*
cf/
3.3 Complex Values
rectangular-cf
polar-cf
complex-cf?
cf-number?
real-part-cf
imaginary-part-cf
conjugate-cf
magnitude-squared-cf
magnitude-cf
angle-cf
hyperbolic-arcsine-cf
hyperbolic-arccosine-cf
hyperbolic-arctangent-cf
in-real-terms
in-imaginary-terms
lift-to-complex
3.4 Algebraic Reals
algebraic-cfs
3.5 Controlling Behavior
consume-limit
cf-procedure-consume-limit
precision-emit
precision
3.6 Arithmetic Caveats
3.7 Emitting Terms in Specific Number Bases
base-emit
base-consume

3 Use🔗ℹ

3.1 Provided 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)
or, more simply, with in-terms: (for/list ((term (in-terms (exp-cf 1/2) 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.

Note that functions as of 1.0 now accept continued fraction arguments.

procedure

(rational->cf n) → continued-fraction?

  n : exact?
Creates a continued fraction representation of the argument.

procedure

(cf-terms->rational cf) → rational?

  cf : (listof (and/c number? exact?))
Interprets a list of terms as a continued fraction expansion of a rational, and gives that rational.

procedure

(continued-fraction? v) → boolean?

  v : any/c
Determines whether the argument is a continued fraction.

procedure

(phi-cf) → continued-fraction?

A continued fraction of the golden ratio.

procedure

(pi-cf) → continued-fraction?

A continued fraction of pi.

procedure

(exp-cf x) → continued-fraction?

  x : (or/c exact? continued-fraction?)
A continued fraction of the natural exponential function.
A continued fraction of the natural logarithm function.
A continued fraction for the base-b logarithm function.

procedure

(sine-cf x) → continued-fraction?

  x : (or/c exact? continued-fraction?)
Standard trigonometric functions.
Standard hyperbolic functions.
The inverse tangent, with the ranges of atan: one argument gives a value in (-pi/2, pi/2), two arguments (the second is the horizontal coordinate, as in atan) a value in (-pi, pi]. Both zero is a division by zero error. With a continued fraction for x that is exactly zero but not known to be (so it never ends), the quotient is infinity and the result is pi/2, whatever the sign of y.
The inverse sine, in [-pi/2, pi/2], and the inverse cosine, in [0, pi]. An argument outside [-1, 1] is a "Complex root" error. A continued fraction whose first term is 1 is not refused, since it may be exactly 1; if it is larger, the error comes from the square root inside.

procedure

(expt-cf base exponent) → continued-fraction?

  base : (or/c exact? continued-fraction?)
  exponent : (or/c exact? continued-fraction?)
Like expt.

procedure

(sqrt-cf x) → continued-fraction?

  x : (or/c exact? continued-fraction?)
Like sqrt.

procedure

(in-terms x [n]) → sequence?

  x : (or/c exact? continued-fraction?)
  n : (or/c exact-nonnegative-integer? +inf.0) = +inf.0
A sequence of the terms of x, at most n of them, for use in any for form. It stops at the end of a finite continued fraction and never reads past the term it delivers.

procedure

(in-convergents x [n]) → sequence?

  x : (or/c exact? continued-fraction?)
  n : (or/c exact-nonnegative-integer? +inf.0) = +inf.0
A sequence of the convergents of x as exact rationals, at most n of them. The last convergent of a finite continued fraction is its value.

Examples:
> (for/list ((t (in-terms (arctangent-cf 1) 6))) t)

'(0 1 3 1 1 1)

> (for/list ((c (in-convergents (pi-cf) 4))) c)

'(3 22/7 333/106 355/113)

procedure

(champernowne-cf base) → continued-fraction?

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

3.2 Arithmetic Procedures🔗ℹ

procedure

(cf+ v ...) → continued-fraction?

  v : (or/c exact-integer? continued-fraction?)

procedure

(cf- v ...) → continued-fraction?

  v : (or/c exact-integer? continued-fraction?)

procedure

(cf* v ...) → continued-fraction?

  v : (or/c exact-integer? continued-fraction?)

procedure

(cf/ v ...) → continued-fraction?

  v : (or/c exact-integer? continued-fraction?)
Standard arithmetic procedures for continued fractions. Every result is a continued fraction, even when all the arguments are integers.

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)

3.3 Complex Values🔗ℹ

A complex value is a pair of real values. Every function above, and the arithmetic procedures, accepts one, and returns a complex value when its argument is complex, as Racket’s own functions do. A complex value is one of
  • a Racket exact complex number such as 1/2+3/4i, the analogue of an exact rational, for which the arithmetic is exact;

  • an opaque pair, printed #<complex-cf>, at least one of whose parts is a continued fraction. It is not a continued-fraction? and not a sequence: look at its parts with real-part-cf and imaginary-part-cf, or in-real-terms and in-imaginary-terms;

  • the empty continued fraction, if a part is infinite. There is one infinity.

No part is ever read to find out whether it collapses to a real or an exact value: (exp-cf (rectangular-cf 0 (pi-cf))) is a pair whose real part guesses -1 and whose imaginary part guesses 0, with the usual consume-limit messages. Branch cuts are Racket’s: the principal value, on the side Racket chooses; only an argument with a literal exact part can land on one.

procedure

(rectangular-cf re im)

 → (or/c exact-rational? complex? continued-fraction? complex-cf?)
  re : (or/c exact-rational? continued-fraction?)
  im : (or/c exact-rational? continued-fraction?)
Makes a complex value, as a real value if im is a literal exact 0 and an exact complex number if both parts are exact. A part known to be the empty continued fraction, without reading it, makes the result the empty continued fraction.

procedure

(polar-cf r theta) → any/c

  r : (or/c exact-rational? continued-fraction?)
  theta : (or/c exact-rational? continued-fraction?)
The value rethetai.

procedure

(complex-cf? v) → boolean?

  v : any/c
Is v a pair with a continued fraction part?

procedure

(cf-number? v) → boolean?

  v : any/c
Is v anything the functions accept: an exact rational, an exact complex number, a continued fraction or a complex pair?

procedure

(real-part-cf z) → any/c

  z : cf-number?
(imaginary-part-cf z) → any/c
  z : cf-number?
(conjugate-cf z) → cf-number?
  z : cf-number?
(magnitude-squared-cf z) → any/c
  z : cf-number?
(magnitude-cf z) → any/c
  z : cf-number?
(angle-cf z) → any/c
  z : cf-number?
The parts of a value (a real value is its own real part, with imaginary part 0), its conjugate, a² + b², the modulus, and the argument in (−π, π]. magnitude-cf of an exact Pythagorean pair such as 3+4i is exactly 5.
The inverse hyperbolic functions, real and complex. The real ones raise an error outside their domains (x < 1 and (abs x) >= 1) unless lift-to-complex is on.

procedure

(in-real-terms z [n]) → sequence?

  z : cf-number?
  n : (or/c exact-nonnegative-integer? +inf.0) = +inf.0

procedure

(in-imaginary-terms z [n]) → sequence?

  z : cf-number?
  n : (or/c exact-nonnegative-integer? +inf.0) = +inf.0
in-terms of the parts of a value. For a real value, the terms of the value and the single term 0.

parameter

(lift-to-complex) → boolean?

(lift-to-complex lift) → void?
  lift : boolean?
 = #f
When true, a real argument that would otherwise raise an error because the result is not real gives a complex result: (sqrt-cf -4), (ln-cf -1), (arcsine-cf 2), (hyperbolic-arccosine-cf 0), (hyperbolic-arctangent-cf 2), and expt-cf of a negative base to an exponent with an even denominator (or a continued fraction exponent). The decision is made before the real function is called, by the same first-term rule the real function uses for its errors, so an argument whose first term is 1 or −1 (the bracket touches the domain) is left to the real function, and an error from inside it is not lifted. (expt-cf -8 1/3) is still the real cube root -2. The default is #f: nothing changes for programs that never set it.

3.4 Algebraic Reals🔗ℹ

A polynomial here is given as a list of their coefficients from the constant term to the most significant. A zero polynomial is an error.
For a given polynomial finds a list of continued fractions for the roots. The list is in order from smallest to largest, and come as (list cf multiplicity).

If the polynomial has no real roots, it evaluates to null.

Examples:
> (define p (list -2 0 1))
> (let ((roots (algebraic-cfs p)))
    (values (for/list ((t (car (car roots)))
                       (i (in-range 6)))
              t)
            (for/list ((t (car (car (cdr roots))))
                       (i (in-range 6)))
              t)))

'(-2 1 1 2 2 2)

'(1 2 2 2 2 2)

3.5 Controlling 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.
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. When providing a continued fraction value to a continued fraction procedure, the continued fraction procedure has its own limit. These parameters are 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.

parameter

(precision) → exact-positive-integer?

(precision n) → void?
  n : exact-positive-integer?
 = (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.

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 (expt 2 30)))
     (for/list ((t (precision-emit (pi-cf)))
                (i (in-range 40)))
       t)))

(3 7 15 1 292 1)

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

3.6 Arithmetic 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 generally produce errors. We expect anything divided by zero to be '(), zero divided by anything to be '(0), and zero divided by zero to be an error, provided both zeros are exact?.

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

consume limit reached

'()

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

'()

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

'()

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

cf/: undefined: 0/0

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:
> (for/list ((t (cf* (sqrt-cf 2) (sqrt-cf 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 yields the rational with the smallest number of terms in the range that the answer is known to lie in. Thus, in the calculation of (cf* (sqrt-cf 2) (sqrt-cf 2)) above the guess made by the library is '(2) rather than something like '(2 10000000000000000000000) or '(1 1 99999999999999999999999).

3.7 Emitting Terms in Specific Number Bases🔗ℹ

procedure

(base-emit v b) → continued-fraction?

  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

(base-consume s b) → continued-fraction?

  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)