2 Use
Link to this section with
@secref["Use" #:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Use" #:doc '(lib "continued-fractions/cf-manual.scrbl")]
2.1 Provided Continued Fractions
Link to this section with
@secref["Provided_Continued_Fractions"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Provided_Continued_Fractions"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
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
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.
Creates a continued fraction representation of the
argument.
Interprets a list of terms
as a continued fraction expansion of a rational, and gives that rational.
Determines whether the argument is a continued fraction.
A continued fraction of the golden ratio.
A continued fraction of pi.
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.
Standard trigonometric functions.
Standard hyperbolic functions.
Raising exact numbers to exact rational powers.
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.2 Arithmetic Procedures
Link to this section with
@secref["Arithmetic_Procedures"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Arithmetic_Procedures"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Standard arithmetic procedures for continued fractions.
Example:
|
'(1 2 1 2 7 6 3 1 36 2 36 1 4 2 1 1 3 7 1 3) |
2.3 Controlling Behavior
Link to this section with
@secref["Controlling_Behavior"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Controlling_Behavior"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
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. This parameter is the limit used. It is reset for every term the continued
fraction generates. If the limit is reached then:
A message is sent to the current-error-port indicating the fact.
The current state is coerced to a rational and the terms of this rational
become the rest of the sequence.
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:
|
> ~pi |
'(3 7 15 1) |
> (abs (/ (- pi (cf-terms->rational ~pi)))) |
3748629.0878493083 |
|
'(3 7 15 1 292 1) |
|
(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 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.4 Arithmetic Caveats
Link to this section with
@secref["Arithmetic_Caveats"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Arithmetic_Caveats"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
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:
|
consume limit reached | consume limit reached |
|
'() |
|
consume limit reached |
'() |
|
consume limit reached |
'() |
|
'() |
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:
|
consume limit reached |
'(2) |
|
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.5 Emitting Terms in Specific Number Bases
Link to this section with
@secref["Emitting_Terms_in_Specific_Number_Bases"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
Link to this section with
@secref["Emitting_Terms_in_Specific_Number_Bases"
#:doc '(lib "continued-fractions/cf-manual.scrbl")]
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)) |
|
'(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 |
|
'(0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0) |
|
'(0 2) |
|
'(33 2) |
|
'(-1 -1 0 0 -2 -4 -7 -8 -3 -9 -7 0 -9 -1 -5 0 -3 -8 -7 -1) |
Examples:
|
'(3 7 15 1 292 1 1 1 2 1) |
|
'(3 1 4 1 5 9 2 6 5 3) |
|
'(3 7 15 1 292 1 1 1 2 1) |