Mercurial > hg > octave-nkf
changeset 13309:1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
* dec2base.m: Code tweaks for slightly better performance.
Use Octave syntax guidelines on spacing and 'endfor' keyword.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 10 Oct 2011 11:12:34 -0700 |
parents | 9a83ec29aec9 |
children | 583940a28bfd |
files | scripts/strings/dec2base.m |
diffstat | 1 files changed, 11 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/strings/dec2base.m +++ b/scripts/strings/dec2base.m @@ -89,7 +89,7 @@ ## determine number of digits required to handle all numbers, can overflow ## by 1 digit - max_len = round (log (max (max (d), 1)) ./ log (base)) + 1; + max_len = round (log (max (max (d(:)), 1)) / log (base)) + 1; if (nargin == 3) max_len = max (max_len, len); @@ -103,7 +103,7 @@ endfor ## convert digits to symbols - retval = reshape (symbols (digits+1), size (digits)); + retval = reshape (symbols(digits+1), size (digits)); ## Check if the first element is the zero symbol. It seems possible ## that LEN is provided, and is less than the computed MAX_LEN and @@ -111,7 +111,7 @@ ## have a leading zero to remove. But if LEN >= MAX_LEN, we should ## not remove any leading zeros. if ((nargin == 2 || (nargin == 3 && max_len > len)) - && all (retval(:,1) == symbols(1)) && length (retval) != 1) + && length (retval) != 1 && ! any (retval(:,1) != symbols(1))) retval = retval(:,2:end); endif @@ -125,9 +125,9 @@ %! pp = dec2base (b^n+1, b); %! assert (dec2base(b^n, b), ['1',s0,'0']); %! assert (dec2base(b^n+1, b), ['1',s0,'1']); -%! end +%! endfor %! s0 = [s0,'0']; -%! end +%! endfor %!test %! digits='0123456789ABCDEF'; @@ -136,18 +136,19 @@ %! pm = dec2base(b^n-1, b); %! assert (length (pm), n); %! assert (all (pm==digits(b))); -%! end -%! end +%! endfor +%! endfor %!test %! for b = 2:16 %! assert (dec2base (0, b), '0'); -%! end +%! endfor %!assert(dec2base (0, 2, 4), "0000"); %!assert(dec2base (2^51-1, 2), ... %! '111111111111111111111111111111111111111111111111111'); %!assert(dec2base(uint64(2)^63-1, 16), '7FFFFFFFFFFFFFFF'); +%!assert(dec2base([1, 2; 3, 4], 2, 3), ["001"; "011"; "010"; "100"]); %!assert(dec2base({1, 2; 3, 4}, 2, 3), ["001"; "011"; "010"; "100"]); %%Test input validation @@ -158,8 +159,8 @@ %!error dec2base (2i) %!error dec2base (-1) %!error dec2base (1.1) -%!error dec2base (1,"ABA") -%!error dec2base (1,"A B") +%!error dec2base (1, "ABA") +%!error dec2base (1, "A B") %!error dec2base (1, ones(2)) %!error dec2base (1, 1) %!error dec2base (1, 37)