NAME
    cfappr - approximate a real number using continued fractions

SYNOPSIS
    cfappr(x [,eps [,rnd]]) or cfappr(x, n [,rnd])

TYPES
    x		real
    eps		real with abs(eps) < 1, defaults to epsilon()
    n		real with n >= 1
    rnd		integer, defaults to config("cfappr")

    return	real

DESCRIPTION
    If x is an integer or eps is zero, either form returns x.

    If abs(eps) < 1, cfappr(x, eps) returns the smallest-denominator
    number in one of the three intervals, [x, x + abs(eps)],
    [x - abs(eps], x], [x - abs(eps)/2, x + abs(eps)/2].

    If n >= 1 and den(x) > n, cfappr(x, n) returns the nearest above,
    nearest below, or nearest, approximation to x with denominator less
    than or equal to n.	 If den(x) <= n, cfappr(x,n) returns x.

    In either case when the result v is not x, how v relates to x is
    determined by bits 0, 1, 2 and 4 of the argument rnd in the same way as
    these bits are used in the functions round() and appr().  In the
    following y is either eps or n.

		rnd	sign of remainder x - v

		0		sgn(y)
		1		-sgn(y
		2		sgn(x), "rounding to zero"
		3		-sgn(x), "rounding from zero"
		4		+, "rounding down"
		5		-, "rounding up"
		6		sgn(x/y)
		7		-sgn(x/y)

    If bit 4 of rnd is set, the other bits are irrelevant for the eps case;
    thus for 16 <= rnd < 24, cfappr(x, eps, rnd) is the smallest-denominator
    number differing from x by at most abs(eps)/2.

    If bit 4 of rnd is set and den(x) > 2, the other bits are irrelevant for
    the bounded denominator case; in the case of two equally near nearest
    approximations with denominator less than n, cfappr(x, n, rnd)
    returns the number with smaller denominator.   If den(x) = 2,  bits
    0, 1 and 2 of rnd are used as described above.

    If -1 < eps < 1, cfappr(x, eps, 0) may be described as the smallest
    denominator number in the closed interval with end-points x and x - eps.
    It follows that if abs(a - b) < 1, cfappr(a, a - b, 0) gives the smallest
    denominator number in the interval with end-points a and b; the same
    result is returned by cfappr(b, b - a, 0) or cfappr(a, b - a, 1).

    If abs(eps) < 1 and v = cfappr(x, eps, rnd), then
    cfappr(x, sgn(eps) * den(v), rnd) = v.

    If 1 <= n < den(x), u = cfappr(x, n, 0) and v = cfappr(x, n, 1), then
    u < x < v, den(u) <= n, den(v) <= n, den(u) + den(v) > n, and
    v - u = 1/(den(u) * den(v)).

    If x is not zero, the nearest approximation with numerator not
    exceeding n is 1/cfappr(1/x, n, 16).

EXAMPLE
    ; c = config("mode", "frac")
    ; x = 43/30; u = cfappr(x, 10, 0); v = cfappr(x, 10, 1);
    ; print u, v, x - u, v - x, v - u, cfappr(x, 10, 16)
    10/7 13/9 1/210 1/90 1/63 10/7

    ; pi = pi(1e-10)
    ; print cfappr(pi, 100, 16), cfappr(pi, .01, 16), cfappr(pi, 1e-6, 16)
    311/99 22/7 355/113

    ; x = 17/12; u = cfappr(x,4,0); v = cfappr(x,4,1);
    ; print u, v, x - u, v - x, cfappr(x,4,16)
    4/3 3/2 1/12 1/12 3/2

LIMITS
    none

LINK LIBRARY
    NUMBER *qcfappr(NUMBER *q, NUMBER *epsilon, long R)

SEE ALSO
    appr, cfsim

## Copyright (C) 1999  Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License
## as published by the Free Software Foundation.
##
## Calc is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
## Public License for more details.
##
## A copy of version 2.1 of the GNU Lesser General Public License is
## distributed with calc under the filename COPYING-LGPL.  You should have
## received a copy with calc; if not, write to Free Software Foundation, Inc.
## 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##
## @(#) $Revision: 30.1 $
## @(#) $Id: cfappr,v 30.1 2007/03/16 11:10:42 chongo Exp $
## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/cfappr,v $
##
## Under source code control:	1994/09/30 01:23:59
## File existed as early as:	1994
##
## chongo <was here> /\oo/\	http://www.isthe.com/chongo/
## Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
