2 appr - approximate numbers by multiples of a specified number
8 x real, complex, matrix, list
12 return same type as x except that complex x may return a real number
15 Return the approximate value of x as specified by a specific error
16 (epsilon) and config ("appr") value.
18 The default value for y is epsilon(). The default value for z is
19 the current value of the "appr" configuration parameter.
21 If y is zero or x is a multiple of y, appr(x,y,z) returns x. I.e.,
22 there is no "approximation" - the result represents x exactly.
24 In the following it is assumed y is nonzero and x is not a multiple of y.
27 appr(x,y,z) is either the nearest multiple of y greater
28 than x or the nearest multiple of y less than x. Thus, if
29 we write a = appr(x,y,z) and r = x - a, then a/y is an integer
30 and abs(r) < abs(y). If r > 0, we say x has been "rounded down"
31 to a; if r < 0, the rounding is "up". For particular x and y,
32 whether the rounding is down or up is determined by z.
34 Only the 5 lowest bits of z are used, so we may assume z has been
35 replaced by its value modulo 32. The type of rounding depends on
38 z = 0 round down or up according as y is positive or negative,
41 z = 1 round up or down according as y is positive or negative,
44 z = 2 round towards zero, sgn(r) = sgn(x)
46 z = 3 round away from zero, sgn(r) = -sgn(x)
48 z = 4 round down, r > 0
52 z = 6 round towards or from zero according as y is positive or
53 negative, sgn(r) = sgn(x/y)
55 z = 7 round from or towards zero according as y is positive or
56 negative, sgn(r) = -sgn(x/y)
62 z = 10 a/y is even or odd according as x/y is positive or negative
64 z = 11 a/y is odd or even according as x/y is positive or negative
66 z = 12 a/y is even or odd according as y is positive or negative
68 z = 13 a/y is odd or even according as y is positive or negative
70 z = 14 a/y is even or odd according as x is positive or negative
72 z = 15 a/y is odd or even according as x is positive or negative
74 z = 16 to 31 abs(r) <= abs(y)/2; if there is a unique multiple
75 of y that is nearest x, appr(x,y,z) is that multiple of y
76 and then abs(r) < abs(y)/2. If x is midway between
77 successive multiples of y, then abs(r) = abs(y)/2 and
78 the value of a is as given by appr(x, y, z-16).
82 appr(x,y,z) returns the matrix or list indexed in the same way as x,
83 in which each element t has been replaced by appr(t,y,z).
87 Returns appr(re(x), y, z) + appr(im(x), y, z) * 1i
90 If appr(x,y,z) != x, then abs(x - appr(x,y,z)) < abs(y).
92 If appr(x,y,z) != x and 16 <= z <= 31, abs(x - appr(x,y,z)) <= abs(y)/2.
94 For z = 0, 1, 4, 5, 16, 17, 20 or 21, and any integer n,
95 appr(x + n*y, y, z) = appr(x, y, z) + n * y.
97 If y is nonzero, appr(x,y,8)/y = an odd integer n only if x = n * y.
100 ; print appr(-5.44,0.1,0), appr(5.44,0.1,0), appr(5.7,1,0), appr(-5.7,1,0)
103 ; print appr(-5.44,-.1,0), appr(5.44,-.1,0), appr(5.7,-1,0), appr(-5.7,-1,0)
106 ; print appr(-5.44,0.1,3), appr(5.44,0.1,3), appr(5.7,1,3), appr(-5.7,1,3)
109 ; print appr(-5.44,0.1,4), appr(5.44,0.1,4), appr(5.7,1,4), appr(-5.7,1,4)
112 ; print appr(-5.44,0.1,6), appr(5.44,0.1,6), appr(5.7,1,6), appr(-5.7,1,6)
115 ; print appr(-5.44,-.1,6), appr(5.44,-.1,6), appr(5.7,-1,6), appr(-5.7,-1,6)
118 ; print appr(-5.44,0.1,9), appr(5.44,0.1,9), appr(5.7,1,9), appr(-5.7,1,9)
121 ; print appr(-.44,0.1,11), appr(.44,0.1,11), appr(5.7,1,11), appr(-5.7,1,11)
124 ; print appr(-.44,-.1,11),appr(.44,-.1,11),appr(5.7,-1,11),appr(-5.7,-1,11)
127 ; print appr(-.44,0.1,12), appr(.44,0.1,12), appr(5.7,1,12), appr(-5.7,1,12)
130 ; print appr(-.44,-.1,12),appr(.44,-.1,12),appr(5.7,-1,12),appr(-5.7,-1,12)
133 ; print appr(-.44,0.1,15), appr(.44,0.1,15), appr(5.7,1,15), appr(-5.7,1,15)
136 ; print appr(-.44,-.1,15),appr(.44,-.1,15),appr(5.7,-1,15),appr(-5.7,-1,15)
139 ; x = sqrt(7-3i, 1e-20)
140 ; print appr(x,1e-5,0), appr(x,1e-5,1), appr(x,1e-5,2), appr(x,1e-6,3)
141 2.70331-.55488i 2.70332-.55487i 2.70331-.55487i 2.70332-.55488i
147 NUMBER *qmappr(NUMBER *q, NUMBER *e, long R);
148 LIST *listappr(LIST *oldlp, VALUE *v2, VALUE *v3);
149 MATRIX *matappr(MATRIX *m, VALUE *v2, VALUE *v3);
152 round, bround, cfappr, cfsim
154 ## Copyright (C) 1999 Landon Curt Noll
156 ## Calc is open software; you can redistribute it and/or modify it under
157 ## the terms of the version 2.1 of the GNU Lesser General Public License
158 ## as published by the Free Software Foundation.
160 ## Calc is distributed in the hope that it will be useful, but WITHOUT
161 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
162 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
163 ## Public License for more details.
165 ## A copy of version 2.1 of the GNU Lesser General Public License is
166 ## distributed with calc under the filename COPYING-LGPL. You should have
167 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
168 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
170 ## @(#) $Revision: 30.1 $
171 ## @(#) $Id: appr,v 30.1 2007/03/16 11:10:42 chongo Exp $
172 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/appr,v $
174 ## Under source code control: 1994/09/25 17:18:21
175 ## File existed as early as: 1994
177 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
178 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/