2 digit - digit at specified position in a "decimal" representation
10 b integer >= 2, default = 10
16 d(x,n,b) returns the digit with index n in a standard base-b "decimal"
17 representation of x, which may be described as follows:
19 For an arbitrary base b >= 2, following the pattern of decimal (base 10)
20 notation in elementary arithmetic, a base-b "decimal" representation of
21 a positive real number may be considered to be specified by a finite or
22 infinite sequence of "digits" with possibly a "decimal" point
23 to indicate where the fractional part of the representation begins.
24 Just as the digits for base 10 are the integers 0, 1, 2, ..., 9, the
25 digits for a base-b representation are the integers d for which
26 0 <= d < b. The index for a digit position is the count, positively to
27 the left, of the number from the "units" position immediately to the
28 left of the "decimal" point; the digit d_n at position n contributes
29 additively d_n * b^n to the value of x. For example,
31 ; d_2 d_1 d_0 . d_-1 d_-2
35 ; d_2 * b^2 + d_1 * b + d0 + d_-1 * b^-1 + d_-2 * b^-2
37 The sequence of digits has to be infinite if den(x) has a prime factor
38 which is not a factor of the base b. In cases where the representation
39 may terminate, the digits are considered to continue with an infinite
40 string of zeros rather than the other possibility of an infinite
41 sequence of (b - 1)s. Thus, for the above example, d_n = 0 for
42 n = -3, -4, ... Similarly, a representation may be considered to
43 continue with an infinite string of zeros on the left, so that in the
44 above example d_n = 0 also for n >= 3.
46 For negative x, digit(x,n,b) is given by digit(abs(x),n,b); the
47 standard "decimal" representation of this x is a - sign followed by
48 the representation of abs(x).
50 In calc, the "real" numbers are all rational and for these the
51 digits following the decimal point eventually form a recurring sequence.
53 With base-b digits for x as explained above, the integer whose base-b
56 ; b_n+k-1 b_n_k-2 ... b_n,
58 i.e. the k digits with last digit b_n, is given by
60 ; digit(b^-r * x, q, b^k)
62 if r and q satisfy n = q * b + r.
67 ; for (n = 6; n >= -6; n++) print digit(a, n),; print
68 0 1 2 3 4 5 6 7 8 9 0 0 0
70 ; for (n = 6; n >= -6; n--) print digit(a, n, 100),; print
71 0 0 0 0 12 34 56 78 90 0 0 0 0
73 ; for (n = 6; n >= -6; n--) print digit(a, n, 256),; print
74 0 0 0 0 1 226 64 201 251 231 108 139 67
76 ; for (n = 1; n >= -12; n++) print digit(10/7, n),; print
77 ; 0 1 4 2 8 5 7 1 4 2 8 5 7 1
79 ; print digit(10/7, -7e1000, 1e6)
84 The absolute value of the integral part of x is assumed to be less
85 than 2^2^31, ensuring that digit(x, n, b) will be zero if n >= 2^31.
86 The size of negative n is limited only by the capacity of the computer
90 NUMBER * qdigit(NUMBER *q, ZVALUE dpos, ZVALUE base)
95 ## Copyright (C) 1999-2006 Landon Curt Noll
97 ## Calc is open software; you can redistribute it and/or modify it under
98 ## the terms of the version 2.1 of the GNU Lesser General Public License
99 ## as published by the Free Software Foundation.
101 ## Calc is distributed in the hope that it will be useful, but WITHOUT
102 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
103 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
104 ## Public License for more details.
106 ## A copy of version 2.1 of the GNU Lesser General Public License is
107 ## distributed with calc under the filename COPYING-LGPL. You should have
108 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
109 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
111 ## @(#) $Revision: 30.1 $
112 ## @(#) $Id: digit,v 30.1 2007/03/16 11:10:42 chongo Exp $
113 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/digit,v $
115 ## Under source code control: 1995/10/03 10:40:01
116 ## File existed as early as: 1995
118 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
119 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/