Mercurial > hg > octave-nkf
annotate scripts/strings/dec2base.m @ 15063:36cbcc37fdb8
Refactor configure.ac to make it more understandable.
Use common syntax for messages in config.h
Correct typos, refer to libraries in all caps, use two spaces after period.
Follow Autoconf guidelines and place general tests before specific tests.
* configure.ac, m4/acinclude.m4: Use common syntax for messages in config.h
Correct typos, refer to libraries in all caps, use two spaces after period.
Follow Autoconf guidelines and place general tests before specific tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 31 Jul 2012 10:28:51 -0700 |
parents | 5d3a684236b0 |
children | 3542e106c496 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13309
diff
changeset
|
1 ## Copyright (C) 2000-2012 Daniel Calvelo |
3789 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3789 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3789 | 18 |
19 ## -*- texinfo -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
20 ## @deftypefn {Function File} {} dec2base (@var{d}, @var{base}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
21 ## @deftypefnx {Function File} {} dec2base (@var{d}, @var{base}, @var{len}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
22 ## Return a string of symbols in base @var{base} corresponding to |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
23 ## the non-negative integer @var{d}. |
3789 | 24 ## |
25 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
3789 | 27 ## dec2base (123, 3) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
28 ## @result{} "11120" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
3789 | 30 ## @end example |
31 ## | |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
32 ## If @var{d} is a matrix or cell array, return a string matrix with one |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
33 ## row per element in @var{d}, padded with leading zeros to the width of |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
34 ## the largest value. |
3789 | 35 ## |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
36 ## If @var{base} is a string then the characters of @var{base} are used as |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
37 ## the symbols for the digits of @var{d}. Space (' ') may not be used |
3789 | 38 ## as a symbol. |
39 ## | |
40 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## @group |
3789 | 42 ## dec2base (123, "aei") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## @result{} "eeeia" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
44 ## @end group |
3789 | 45 ## @end example |
4492 | 46 ## |
47 ## The optional third argument, @var{len}, specifies the minimum | |
48 ## number of digits in the result. | |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
49 ## @seealso{base2dec, dec2bin, dec2hex} |
3789 | 50 ## @end deftypefn |
51 | |
3791 | 52 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 53 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
54 | |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
55 function retval = dec2base (d, base, len) |
3789 | 56 |
4492 | 57 if (nargin < 2 || nargin > 3) |
6046 | 58 print_usage (); |
3789 | 59 endif |
60 | |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 if (iscell (d)) |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 d = cell2mat (d); |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 endif |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
65 # Create column vector for algorithm |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 if (! iscolumn (d)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
67 d = d(:); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
68 endif |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
69 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 if (! isnumeric (d) || iscomplex (d) || any (d < 0 | d != fix (d))) |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 error ("dec2base: input must be real non-negative integers"); |
3789 | 72 endif |
73 | |
74 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
5443 | 75 if (ischar (base)) |
3789 | 76 symbols = base; |
77 base = length (symbols); | |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
78 if (length (unique (symbols)) != base) |
5407 | 79 error ("dec2base: symbols representing digits must be unique"); |
3789 | 80 endif |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
81 if (any (isspace (symbols))) |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
82 error ("dec2base: whitespace characters are not valid symbols"); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
83 endif |
4030 | 84 elseif (! isscalar (base)) |
5407 | 85 error ("dec2base: cannot convert from several bases at once"); |
3789 | 86 elseif (base < 2 || base > length (symbols)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
87 error ("dec2base: BASE must be between 2 and 36, or a string of symbols"); |
3789 | 88 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 |
5125 | 90 ## determine number of digits required to handle all numbers, can overflow |
91 ## by 1 digit | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
92 max_len = round (log (max (max (d(:)), 1)) / log (base)) + 1; |
4492 | 93 |
94 if (nargin == 3) | |
95 max_len = max (max_len, len); | |
96 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
97 |
3789 | 98 ## determine digits for each number |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
99 digits = zeros (length (d), max_len); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
100 for k = max_len:-1:1 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 digits(:,k) = mod (d, base); |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
102 d = round ((d - digits(:,k)) / base); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
103 endfor |
3789 | 104 |
105 ## convert digits to symbols | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
106 retval = reshape (symbols(digits+1), size (digits)); |
3789 | 107 |
5133 | 108 ## Check if the first element is the zero symbol. It seems possible |
109 ## that LEN is provided, and is less than the computed MAX_LEN and | |
110 ## MAX_LEN is computed to be one larger than necessary, so we would | |
111 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should | |
112 ## not remove any leading zeros. | |
113 if ((nargin == 2 || (nargin == 3 && max_len > len)) | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
114 && length (retval) != 1 && ! any (retval(:,1) != symbols(1))) |
5125 | 115 retval = retval(:,2:end); |
116 endif | |
117 | |
3789 | 118 endfunction |
5125 | 119 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
120 |
5125 | 121 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
122 %! s0 = ""; |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
123 %! for n = 1:13 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
124 %! for b = 2:16 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
125 %! pp = dec2base (b^n+1, b); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
126 %! assert (dec2base (b^n, b), ['1',s0,'0']); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
127 %! assert (dec2base (b^n+1, b), ['1',s0,'1']); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
128 %! endfor |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
129 %! s0 = [s0,'0']; |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
130 %! endfor |
5125 | 131 |
132 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
133 %! digits = "0123456789ABCDEF"; |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
134 %! for n = 1:13 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
135 %! for b = 2:16 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
136 %! pm = dec2base (b^n-1, b); |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
137 %! assert (length (pm), n); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
138 %! assert (all (pm == digits(b))); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
139 %! endfor |
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
140 %! endfor |
5125 | 141 |
142 %!test | |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
143 %! for b = 2:16 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
144 %! assert (dec2base (0, b), '0'); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
145 %! endfor |
5125 | 146 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
147 %!assert (dec2base (0, 2, 4), "0000") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
148 %!assert (dec2base (2^51-1, 2), ... |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
149 %! "111111111111111111111111111111111111111111111111111") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
150 %!assert (dec2base (uint64 (2)^63-1, 16), "7FFFFFFFFFFFFFFF") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
151 %!assert (dec2base ([1, 2; 3, 4], 2, 3), ["001"; "011"; "010"; "100"]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
152 %!assert (dec2base ({1, 2; 3, 4}, 2, 3), ["001"; "011"; "010"; "100"]) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
153 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
154 %%Test input validation |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
155 %!error dec2base () |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
156 %!error dec2base (1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
157 %!error dec2base (1, 2, 3, 4) |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
158 %!error dec2base ("A") |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
159 %!error dec2base (2i) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
160 %!error dec2base (-1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
161 %!error dec2base (1.1) |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
162 %!error dec2base (1, "ABA") |
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
163 %!error dec2base (1, "A B") |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
164 %!error dec2base (1, ones (2)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
165 %!error dec2base (1, 1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
166 %!error dec2base (1, 37) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
167 |