2 cfappr - approximate a real number using continued fractions
5 cfappr(x [,eps [,rnd]]) or cfappr(x, n [,rnd])
9 eps real with abs(eps) < 1, defaults to epsilon()
11 rnd integer, defaults to config("cfappr")
16 If x is an integer or eps is zero, either form returns x.
18 If abs(eps) < 1, cfappr(x, eps) returns the smallest-denominator
19 number in one of the three intervals, [x, x + abs(eps)],
20 [x - abs(eps], x], [x - abs(eps)/2, x + abs(eps)/2].
22 If n >= 1 and den(x) > n, cfappr(x, n) returns the nearest above,
23 nearest below, or nearest, approximation to x with denominator less
24 than or equal to n. If den(x) <= n, cfappr(x,n) returns x.
26 In either case when the result v is not x, how v relates to x is
27 determined by bits 0, 1, 2 and 4 of the argument rnd in the same way as
28 these bits are used in the functions round() and appr(). In the
29 following y is either eps or n.
31 rnd sign of remainder x - v
35 2 sgn(x), "rounding to zero"
36 3 -sgn(x), "rounding from zero"
42 If bit 4 of rnd is set, the other bits are irrelevant for the eps case;
43 thus for 16 <= rnd < 24, cfappr(x, eps, rnd) is the smallest-denominator
44 number differing from x by at most abs(eps)/2.
46 If bit 4 of rnd is set and den(x) > 2, the other bits are irrelevant for
47 the bounded denominator case; in the case of two equally near nearest
48 approximations with denominator less than n, cfappr(x, n, rnd)
49 returns the number with smaller denominator. If den(x) = 2, bits
50 0, 1 and 2 of rnd are used as described above.
52 If -1 < eps < 1, cfappr(x, eps, 0) may be described as the smallest
53 denominator number in the closed interval with end-points x and x - eps.
54 It follows that if abs(a - b) < 1, cfappr(a, a - b, 0) gives the smallest
55 denominator number in the interval with end-points a and b; the same
56 result is returned by cfappr(b, b - a, 0) or cfappr(a, b - a, 1).
58 If abs(eps) < 1 and v = cfappr(x, eps, rnd), then
59 cfappr(x, sgn(eps) * den(v), rnd) = v.
61 If 1 <= n < den(x), u = cfappr(x, n, 0) and v = cfappr(x, n, 1), then
62 u < x < v, den(u) <= n, den(v) <= n, den(u) + den(v) > n, and
63 v - u = 1/(den(u) * den(v)).
65 If x is not zero, the nearest approximation with numerator not
66 exceeding n is 1/cfappr(1/x, n, 16).
69 ; c = config("mode", "frac")
70 ; x = 43/30; u = cfappr(x, 10, 0); v = cfappr(x, 10, 1);
71 ; print u, v, x - u, v - x, v - u, cfappr(x, 10, 16)
72 10/7 13/9 1/210 1/90 1/63 10/7
75 ; print cfappr(pi, 100, 16), cfappr(pi, .01, 16), cfappr(pi, 1e-6, 16)
78 ; x = 17/12; u = cfappr(x,4,0); v = cfappr(x,4,1);
79 ; print u, v, x - u, v - x, cfappr(x,4,16)
86 NUMBER *qcfappr(NUMBER *q, NUMBER *epsilon, long R)
91 ## Copyright (C) 1999 Landon Curt Noll
93 ## Calc is open software; you can redistribute it and/or modify it under
94 ## the terms of the version 2.1 of the GNU Lesser General Public License
95 ## as published by the Free Software Foundation.
97 ## Calc is distributed in the hope that it will be useful, but WITHOUT
98 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
99 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
100 ## Public License for more details.
102 ## A copy of version 2.1 of the GNU Lesser General Public License is
103 ## distributed with calc under the filename COPYING-LGPL. You should have
104 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
105 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
107 ## @(#) $Revision: 30.1 $
108 ## @(#) $Id: cfappr,v 30.1 2007/03/16 11:10:42 chongo Exp $
109 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/cfappr,v $
111 ## Under source code control: 1994/09/30 01:23:59
112 ## File existed as early as: 1994
114 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
115 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/