annotate scripts/strings/dec2base.m @ 9051:1bf0ce0930be

Grammar check TexInfo in all .m files Cleanup documentation sources to follow a few consistent rules. Spellcheck was NOT done. (but will be in another changeset)
author Rik <rdrider0-list@yahoo.com>
date Fri, 27 Mar 2009 22:31:03 -0700
parents eb63fbe60fab
children 923c7cb7f13f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
1 ## Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2009 Daniel Calvelo
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
2 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
4 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## your option) any later version.
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
9 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
14 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
18
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
20 ## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}, @var{len})
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6967
diff changeset
21 ## Return a string of symbols in base @var{b} corresponding to
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
22 ## the nonnegative integer @var{n}.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
23 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
24 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## @group
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
26 ## dec2base (123, 3)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
27 ## @result{} "11120"
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
28 ## @end group
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
29 ## @end example
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
30 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
31 ## If @var{n} is a vector, return a string matrix with one row per value,
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
32 ## padded with leading zeros to the width of the largest value.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
33 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
34 ## If @var{b} is a string then the characters of @var{b} are used as
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
35 ## the symbols for the digits of @var{n}. Space (' ') may not be used
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
36 ## as a symbol.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
37 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
38 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
39 ## @group
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
40 ## dec2base (123, "aei")
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
41 ## @result{} "eeeia"
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
42 ## @end group
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
43 ## @end example
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
44 ##
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
45 ## The optional third argument, @var{len}, specifies the minimum
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
46 ## number of digits in the result.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5443
diff changeset
47 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex}
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
48 ## @end deftypefn
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
49
3791
c1c532a0acb2 [project @ 2001-02-09 15:28:23 by jwe]
jwe
parents: 3789
diff changeset
50 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
51 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
52
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
53 function retval = dec2base (n, base, len)
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
54
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
55 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
56 print_usage ();
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
57 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
58
6967
65a28e9de0a5 [project @ 2007-10-06 12:47:11 by jwe]
jwe
parents: 6046
diff changeset
59 if (numel (n) != length (n))
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
60 n = n(:);
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
61 elseif (any (n < 0 | n != fix (n)))
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
62 error ("dec2base: can only convert non-negative integers");
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
63 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
64
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
65 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5407
diff changeset
66 if (ischar (base))
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
67 symbols = base;
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
68 base = length (symbols);
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
69 if any (diff (sort (toascii (symbols))) == 0)
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
70 error ("dec2base: symbols representing digits must be unique");
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
71 endif
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3791
diff changeset
72 elseif (! isscalar (base))
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
73 error ("dec2base: cannot convert from several bases at once");
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
74 elseif (base < 2 || base > length (symbols))
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
75 error ("dec2base: base must be between 2 and 36 or a string of symbols");
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
76 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
77
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
78 ## determine number of digits required to handle all numbers, can overflow
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
79 ## by 1 digit
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
80 max_len = round (log (max (max (n), 1)) ./ log (base)) + 1;
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
81
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
82 if (nargin == 3)
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
83 max_len = max (max_len, len);
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
84 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
85
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
86 ## determine digits for each number
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
87 power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0));
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
88 n = n(:) * ones (1, max_len);
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
89 digits = floor (rem (n, base*power) ./ power);
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
90
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
91 ## convert digits to symbols
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
92 retval = reshape (symbols (digits+1), size (digits));
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
93
5133
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
94 ## Check if the first element is the zero symbol. It seems possible
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
95 ## that LEN is provided, and is less than the computed MAX_LEN and
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
96 ## MAX_LEN is computed to be one larger than necessary, so we would
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
97 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
98 ## not remove any leading zeros.
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
99 if ((nargin == 2 || (nargin == 3 && max_len > len))
5400
c7e3cf2fce3e [project @ 2005-07-05 15:01:32 by jwe]
jwe
parents: 5307
diff changeset
100 && all (retval(:,1) == symbols(1)) && length (retval) != 1)
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
101 retval = retval(:,2:end);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
102 endif
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
103
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
104 endfunction
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
105
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
106 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
107 %! s0='';
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
108 %! for n=1:13
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
109 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
110 %! pp=dec2base(b^n+1,b);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
111 %! assert(dec2base(b^n,b),['1',s0,'0']);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
112 %! assert(dec2base(b^n+1,b),['1',s0,'1']);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
113 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
114 %! s0=[s0,'0'];
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
115 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
116
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
117 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
118 %! digits='0123456789ABCDEF';
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
119 %! for n=1:13
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
120 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
121 %! pm=dec2base(b^n-1,b);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
122 %! assert(length(pm),n);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
123 %! assert(all(pm==digits(b)));
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
124 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
125 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
126
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
127 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
128 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
129 %! assert(dec2base(0,b),'0');
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
130 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
131
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
132 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
133 %! assert(dec2base(2^51-1,2),
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
134 %! '111111111111111111111111111111111111111111111111111');